Hi,
I'm trying to build a very simple game with UDP just for testing.
Unfortunately at the moment it works worse than it used to work with TCP...
I'm not exactly sure, what the problem is, I thought that you maybe can help me.
Here is the full code:
http://pastebin.com/2ERzSk1nor
http://codepad.org/qsznZjow And here are the two most important passages:
void Game::sendGameState()
{
sf::Packet toSend;
toSend << myServerPlayer.xPos << myServerPlayer.yPos << myServerPlayer.input
<< myClientPlayer.xPos << myClientPlayer.yPos;
sf::Socket::Status status = mySocket.Send(toSend, myIPAddress, PORT);
switch(status)
{
case sf::Socket::Disconnected:
cout << "Disconnected" << endl;
myIsConnected = false;
break;
case sf::Socket::Error:
cout << "Error sending" << endl;
break;
case sf::Socket::Done:
break;
case sf::Socket::NotReady:
//try again next time;
break;
}
}
void Game::receiveAndSyncGameState()
{
sf::Packet toReceive;
unsigned short port = PORT;
sf::Socket::Status status = mySocket.Receive(toReceive, myIPAddress, port);
switch(status)
{
case sf::Socket::Disconnected:
cout << "Disconnected" << endl;
myIsConnected = false;
break;
case sf::Socket::Error:
cout << "Error receiving game state" << endl;
break;
case sf::Socket::Done:
toReceive >> myServerPlayer.xPos >> myServerPlayer.yPos >> myServerPlayer.input
>> myClientPlayer.xPos >> myClientPlayer.yPos;
//no interpolation yet
break;
case sf::Socket::NotReady:
//try again next time;
break;
}
}
I'm sending gamestate data periodically from the server to the client and the client sends input data periodically to the server.
In the current code it is sent 100 times per second, which is quite huge, but I tried it with 20/s and it didn't work very well too.
At client side the rectangle controlled by the server is always "jumping" between two positions which aren't close to each other.
And after some time there also is quite big delay and some socket errors occur.
I'm not sure if the "jumping" is caused by UDP specific problems, maybe it is caused by lack of mechanism which ensure that data arrives correctly and in the right order?
Or it is just some silly bug i build into my code?
But there is also the delay... sending 20 times per second ~10bytes with UDP over LAN should be no problem, should it?
And I'm also not sure, if I'm using the asnyc sockets in the right way...
I can upload the .exe file if you want/need it.
It would be very nice, if you can help me :-)