Hi, I'm new here, hello to everybody:)
I'm trying to send strings from Java to an SFML application, via TCP, and I want to use the packet abstraction, which I found nice.
Java-wise, the code is this:
String message
= "something";DataOutputStream dos
= new DataOutputStream(socket.
getOutputStream());int length
= message.
getBytes().
length + 4;dos.
writeInt(length
);dos.
writeBytes(message
); dos.
flush(); In SFML, the following code doesn't work:
sf::Packet packet;
socket.receive(packet);
std::cout << "Packet size: " << packet.getDataSize() << std::endl;
std::string stringa;
packet >> stringa;
std::cout << stringa << std::endl;
The problem is that packet.getDataSize() returns 0, and subsequently nothing is received.
In order to debug this, I tried to call directly socket.receive, and I discovered that it receives 1 byte at a time, even if I specify 4 of them (to receive the first int with the size of the packet);
So, to properly receive the packet I wrote this:
char *dataSize = new char[4];
std::size_t received = 0;
std::size_t receivedSoFar = 0;
do {
socket.receive((void *) (dataSize + receivedSoFar), 1, received);
receivedSoFar += received;
} while (receivedSoFar < 4);
then I convert the 4 char to a number, I allocate a proper buffer, and then I happily receive, with the same mechanism, the rest of the string.
I also noticed that socket.receive, when called on a connection coming from a C++ SFML application, does indeed receives 4 bytes at a time. Maybe it's related to the fact that when receiving a packet sent from Java, only the first byte is read, then it's treated as a whole int and as such its value is zero.
I'm running SFML RC1 as packaged in ArchLinux.
Bye:)