• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Shawn619

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

7 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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/)

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0