Hi, I have some really simple code that sends 10 consecutive numbers.
Sender:
int main()
{
std::cout << "Sending\n";
sf::UdpSocket socket;
socket.bind(8041);
socket.setBlocking(true);
sf::IpAddress loopback("127.0.0.1");
for(int i = 0; i < 100; i++)
{
sf::Packet packet;
packet << sf::Int32(i);
socket.send(packet, loopback, 8040);
}
std::cout << "Done sending\n";
}
Receiver:
int main()
{
std::cout << "Ready\n";
sf::UdpSocket socket;
socket.setBlocking(true);
socket.bind(8040);
while(1)
{
sf::Packet packet;
sf::IpAddress address;
unsigned short port;
socket.receive(packet, address, port);
std::cout << "Packet received from " << address.toString() << " port: " << port << "\n";
sf::Int32 i;
packet >> i;
std::cout << "Message: " << i << "\n";
}
}
When I turn on the receiver, then run my sender, I get this output on the receiver's console:
Ready
Packet received from 127.0.0.1 port: 8041
Message: 0
instead of 100 messages from 0 to 99, which is what I expect.
When I add this line of code to add a slight delay in the sender
sf::sleep(sf::Time(sf::milliseconds(1)));
right after this line: socket.send(packet, loopback, 8040);
I get every message, except the 2nd message ("Message: 1"), gets mysteriously lost most, but not all of the time. Why do I need this delay here? And any clues on why only the second message keeps getting lost consistently?