It has been a while since i last gave SFML networking a try, i wanted to give it a go after using other API's (Qt Networking\Boost::Asio)
There is a few things i don't understand, let me give a few examples.
Packet sizes:
Sending a sample with 999 bytes, to a receiver (server) reads the size of the data randomly, either less than 999 bytes (still within MTU!) or strangely more often with more data than i specified...
Code of sender....
sf::TcpSocket socket;
if (socket.Connect("localhost", 25001) == sf::TcpSocket::Done) {
printf("Connected to server\n");
char packet[1024] = "";
for (int i = 0; i < 100; i++) {
memset(packet, 0x01, 999);
if (sf::TcpSocket::Done == socket.Send(packet, 999)) {
printf("Packet sent to server\n");
}
else {
printf("Failed to send packet to server\n");
}
}
}
else {
printf("Failed to connect to server\n");
}
Code of receiver:
while (true) {
size_t receivedSize = 0;
sf::TcpSocket::Status status = clientSocket->Receive(packet, sizeof(packet), receivedSize);
if (status == sf::TcpSocket::Done) {
handleClientPacket(packet, receivedSize);
}
else if (status == sf::TcpSocket::Error ||
status == sf::TcpSocket::Disconnected) {
printf("Packet error or disconnection\n");
break;
}
else {
// Not yet ready
sf::Sleep(10);
}
}
void Server::handleClientPacket(const char *data, size_t size)
{
printf("Packet %d with size %u\n", m_packetCount++, size);
}
results:
...
Packet 34 with size 999
Packet 35 with size 999
Packet 36 with size 999
Packet 37 with size 999
Packet 38 with size 999
Packet 39 with size 999
Packet 40 with size 999
Packet 41 with size 999
Packet 42 with size 999
Packet 43 with size 999
Packet 44 with size 999
Packet 45 with size 999
Packet 46 with size 999
Packet 47 with size 1024
Packet 48 with size 1024
Packet 49 with size 1024
Packet 50 with size 1024
Packet 51 with size 1024
Packet 52 with size 1024
Packet 53 with size 1024
Packet 54 with size 1024
Packet 55 with size 1024
Packet 56 with size 1024
Packet 57 with size 1024
Packet 58 with size 1024
Packet 59 with size 1024
Packet 60 with size 1024
Packet 61 with size 1024
Packet 62 with size 624
Packet 63 with size 999
Packet 64 with size 999
Packet 65 with size 999
Packet 66 with size 999
Packet 67 with size 1024
etc... very strange, my questions is why? I expected 999.. yes OS socket buffer was filled up at some point, i want to know why the socket wrote more than what i specified...
I have a few other remarks but lets start with this one..