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

Author Topic: Get Number of Bytes before Receiving  (Read 2644 times)

0 Members and 1 Guest are viewing this topic.

isReady

  • Newbie
  • *
  • Posts: 13
    • View Profile
Get Number of Bytes before Receiving
« on: December 12, 2012, 07:56:35 pm »
Good evening,
I am new to SFML and want to write a network (server) application which works with Linux and Windows. Certainly the client is not written in C++ so I can't use the sf::Packet-class because I don't know how to implement the receive-funktion in other programming languages.
Therefore i want to send/receive std::strings. Unfortunately I don't know how many bytes I gonna receive when using socket selectors.
//Note: this snippet isn't tested but it shows my problem
std::string sData1;
std::size_t BytesReceived, BytesToReceive = 300;
sData.resize(301); //not nice. I would like to know how many bytes i need to reserve BEFORE calling the receive-method
client.receive((void*)sData1.c_str(), BytesToReceive, BytesReceived);
mfg isReady

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get Number of Bytes before Receiving
« Reply #1 on: December 12, 2012, 09:24:10 pm »
You must first send the size if you want to be able to pre-allocate the string.

Otherwise, receive small chunks in a fixed-size buffer, in a loop, and append it to your final string until you reach the end.
Laurent Gomila - SFML developer

isReady

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Get Number of Bytes before Receiving
« Reply #2 on: December 13, 2012, 09:27:27 pm »
Thank you, that seems to be a good solution. I thought there would be something like the select-function of the WinApi which returns the number of bytes waiting in line.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get Number of Bytes before Receiving
« Reply #3 on: December 13, 2012, 09:38:31 pm »
TCP is a stream protocol, there are no boundaries. If there are 30 bytes available to be received, and you're waiting for a string, you can't be sure that these 30 bytes are your string: it could be a part of it, or it plus the beginning of the next data. So you have to handle the string end explicitely, either by sending the size first, or by sending the terminating '\0'.
Laurent Gomila - SFML developer

isReady

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Get Number of Bytes before Receiving
« Reply #4 on: December 14, 2012, 01:54:18 pm »
Thank you, I understood it now. I've found this snippet in the documentation and think that I might have found a bug.
sf::SocketSelector

             sf::TcpSocket* client = new sf::TcpSocket;
             if (listener.accept(*client) == sf::Socket::Done)
             {
                 // Add the new client to the clients list
                 clients.push_back(client);

                 // Add the new client to the selector so that we will
                 // be notified when he sends something
                 selector.add(*client);
             }

If the listener.accept()-Method fails there is a memory leak because the client-pointer won't get deallocated.

             sf::TcpSocket* client = new sf::TcpSocket;
             if (listener.accept(*client) == sf::Socket::Done)
             {
                 // Add the new client to the clients list
                 clients.push_back(client);

                 // Add the new client to the selector so that we will
                 // be notified when he sends something
                 selector.add(*client);
             }
             else
             {
                 delete client;
             }

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Get Number of Bytes before Receiving
« Reply #5 on: December 14, 2012, 03:01:50 pm »
Oh! You're right :)
Laurent Gomila - SFML developer

 

anything