Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Putting messages on packets.  (Read 2147 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Putting messages on packets.
« on: April 26, 2014, 01:26:11 pm »
Hi, I'm on ubuntu.

I would like to put contents on packet with the operator <<.

But it doesn't seems to work.

sf::Packet packet;
for (unsigned int i = 0; i < texPaths.size(); i++) {
        packet<<texPaths[i];
        if (i != texPaths.size() - 1)
               packet<<"*";
}                
std::string message;
packet>>message;
std::cout<<"message : "<<message<<std::endl;
 

Is it normal that only the first std::string of my std::vector si putted into the packet ?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Putting messages on packets.
« Reply #1 on: April 26, 2014, 01:50:36 pm »
You're only extracting one string.  You need to extract all of them.  You may need to put the vector's size into your packet so the receiver knows how many strings to extract.

If it helps, I think binary's SFNUL library (http://en.sfml-dev.org/forums/index.php?topic=13723.0) has a Message class that improves on Packet by--among other things--allowing you to insert/extract STL containers directly.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Putting messages on packets.
« Reply #2 on: April 26, 2014, 04:04:48 pm »
Ha ok but I've solved this problem by putting all the strings of my std::vector into a single string.
if (odfaeg::Network::hasRequest()) {
            odfaeg::User* user;
            std::string request = odfaeg::Network::getLastRequest(&user);
            odfaeg::SymEncPacket packet;
            std::string response = "";
            if (request == "GETRESOURCESPATHS") {

                for (unsigned int i = 0; i < texPaths.size(); i++) {
                    response += texPaths[i];
                    if (i != texPaths.size() - 1)
                        response += "*";
                }
                packet<<response;
                std::string message;
                packet>>message;
                user->sendTcpPacket(packet);
            }
        }
 

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Putting messages on packets.
« Reply #3 on: April 26, 2014, 11:59:29 pm »
You've "fixed" the requirement, not your implementation...
SFML / OS X developer

 

anything