Jump to content

  • Log In with Google      Sign In   
  • Create Account


Winsock(C++) - TCP/IP Multiplayer freezes after 5 seconds


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 29 December 2013 - 08:45 PM

I open 2 clients, 1 first makes a game room, then the other client joins this room.

 

10 Game rooms @ 2 Players each

 

Each player represents a line controlled by keyboard inputs, this line data is sent to the server, stored on the server, then the server returns the other user's line data.

 

The communication is flawlessly synchronized for about 5 seconds, then the host client freezes and is unresponsive.

 

990u41.jpg

 

 

The looping communication once the game room is entered is this for the client:

 

Client:

 
serverUpdate(){
prepare information into data structure
 
//send this data structure to server for server-side storage
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf)+2, 0 );
 
//get other players data
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
 
update other player movement with this received data
}
 
serverUpdate() is called every 30ms
 

Server:

 

in thread function:

 
//after a socket connection is establish and game room joined,
while(true){
 
iResult = recv(((ClientParams*)lpParam)->getSocket(), recvbuf, recvbuflen, 0);
 
Receive and store user data
 
iSendResult = send( ((ClientParams*)lpParam)->getSocket(), temp.c_str(), DEFAULT_BUFLEN, 0 );
 
Send other user's data
 
}
 

These are all blocking-sockets if that makes a difference with no other settings (ie: TCP_NODELAY)


Edited by Shawn619, 30 December 2013 - 04:04 AM.


Sponsor:

#2 jsaade   Members   -  Reputation: 197

Like
0Likes
Like

Posted 30 December 2013 - 02:55 AM

The last time i did any socket programming was 5 years ago, but your code would block if there is no data on the socket (freeze) as you said you are using blocking sockets.

Ideally you should poll (with select) and wait for data before calling recv.



#3 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 30 December 2013 - 04:12 AM

I've changed my sockets to non-blocking and it no longer freezes, but the delay grows exponentially over time while also giving me the winsock 10035 error(buffer given too many requests i believe?)

 

So now i will implement the select(), which i have read the documentation on but im still hazy on the function.

 

Does the select() basically halt winsock's recv buffer from accepting anymore data until the timeout has been reached?



#4 jsaade   Members   -  Reputation: 197

Like
0Likes
Like

Posted 30 December 2013 - 04:40 AM

I am assuming you are sending small packets over TCP. So in time, TCP would be stacking the packets until enough data is available.

You should set TCP_NODELAY.

 

In the last network engine I worked on (general purpose), I had thread dedicated for socket polling, getting the data and transmitting the data message to the game thread.

 

If you have a turn based game, TCP is fine, but if you need to have a realtime game, you need to consider UDP (http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/)



#5 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 30 December 2013 - 05:01 AM

Yeah, unfortunately misinformation led me to believe that TCP was for real-time games. However, i have a small amount of people per room(2-4) so it's not "that" bad for my set-up and provides very playable latency caused by TCP. I don't feel like rewriting the 500 lines i have into the TCP networking portion of my game ill keep useing TCP for now for this project.



#6 hplus0603   Moderators   -  Reputation: 4907

Like
1Likes
Like

Posted 30 December 2013 - 11:06 AM

TCP is fine for real-time-ish games, like World of Warcraft, or Starcraft. It's not as good for hair-trigger games like Quake or Counter-Strike.

Given your symptoms, it's likely that you are building up latency between server and client; not draining the client at the same speed that you're sending from the server. In general, a receiving end needs to receive "as much as it can" each time, and then look through the stream to pick out completely received messages and dispatch them, keeping the rest (beginning of the next message, perhaps) in a buffer until the next time through when more data has arrived.
enum Bool { True, False, FileNotFound };

#7 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 30 December 2013 - 09:30 PM

I'm not sure if im using select() entirely correctly on the client side:

 

Should i be using select() on reading and writing to my server connection?

while(true){
// clear the set ahead of time
FD_ZERO(&readfds);
FD_ZERO(&writefds);
 
// add our descriptors to the set
FD_SET(ConnectSocket, &readfds);
FD_SET(ConnectSocket, &writefds);
 
// wait until either socket has data ready to be recv()d (timeout 10 secs)
tv.tv_sec = 10;
 
//select
int rv = select(ConnectSocket+1, &readfds, &writefds, NULL, &tv);
 
if (rv == -1) {
    perror("select"); // error occurred in select()
}else if (rv == 0){
    printf("Timeout occurred! No data after 10 seconds.\n");
}else{
//perror("success");
// one or both of the descriptors have data
 
if (FD_ISSET(ConnectSocket, &writefds)) {
    //receive and store user data
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf)+2, 0 );
}
if (FD_ISSET(ConnectSocket, &readfds)) {
    //receive and store user data
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    
    std::stringstream ss(recvbuf);
    ss >> otherUser.posx1;
    ss.ignore();
    ss >> otherUser.posx2;
    ss.ignore();
    ss >> otherUser.posy1;
    ss.ignore();
    ss >> otherUser.posy2;
}
}

I didn't use select() on the server side because it gives me 1million timeout errors every millisecond(even if the timeout is 10 seconds).
 
If i eyeball the latency it looks like it fluctuates around a 0.5 second delay, which actually isn't too bad, but i need to know if this is due to TCP and if UDP would give me a much smaller delay?

Edited by Shawn619, 30 December 2013 - 09:34 PM.


#8 hplus0603   Moderators   -  Reputation: 4907

Like
0Likes
Like

Posted 30 December 2013 - 11:38 PM

If you are using TCP, you are using recv() and send() all wrong. They are not guaranteed to read or write the entire amount of data that you request it to. Thus, you have to keep your own buffer for outgoing data, and only remove the data that was actually sent, and receive whatever is there (which may be less or more than one particular call to send() from the other end.)

Other bugs in your program include sending each time through the loop, rather than just on a fixed timer, and assuming that the data received by any particular call to recv() will be zero terminated and thus suitable as C-style string.

I also don't see you clear the tv_usec field of the timeval.

TCP and UDP will have the same speed/delay if there is no packet drop and you use them each correctly.
TCP will, however, delay delivering some data it has received, if it hasn't yet received earlier data. Also, TCP will deliver all the data in order, but may chunk it up differently -- you may send() 10 bytes and send() 20 bytes, and then recv() 1 byte and the next time recv() 29 bytes.
UDP will deliver a whole message, zero or more times, with no guarantee of uniqueness, delivery, or order.
enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS