So I have some code for a 2-player game that looks like this for both the host and client player, using a non-blocking SocketUDP:
sf::Socket::Status sendStatus;
sf::Socket::Status rcvStatus;
if (socket.IsValid())
{
// SEND position data to the other player
sf::Packet outPacket;
outPacket << myPlayer.getX() << myPlayer.getY();
sf::IPAddress tip = theirIP;
sendStatus = socket.Send(outPacket, tip, PORT);
// RECEIVE position data from the other player
float x = theirPlayer.getX();
float y = theirPlayer.getY();
sf::Packet inPacket;
unsigned short port = PORT;
tip = theirIP;
rcvStatus = socket.Receive(inPacket, tip, port);
if (rcvStatus == sf::Socket::Done)
{
if (!(inPacket >> x >> y))
{
// Error...
}
else
{
theirPlayer.SetPosition(x, y);
}
}
}
else
{
// Error. Quit game.
}
Both players are just sending their positions to one another.
Initially I had the framerate capped at 120 FPS, and this code ran every game loop (inefficient, I know). It would work fine for awhile, but rcvStatus would occasionally be sf::Socket::Error rather than sf::Socket::Done, which is what it usually sits at. The problem seemed to get worse the longer the session was. The problem persisted at 60 FPS.
At a forced limit of 10 FPS I didn't notice any of these errors.
So my hypothesis is that I was just sending out too many packets. Could that cause an sf::Socket::Error condition? Is there any way to find out what triggered an sf::Socket::Error?
I'm using SFML 1.6 and Visual Studio 2010 if that matters.