Sign in to follow this  

Lot's of Lag, need help.

This topic is 4755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I programmed a space game, a spaceship travels around in a window. I use Asynchronous winsock, TCP. It's a server - client multiplayer game. Whenever a client connects to the server I add the connected socket to a client. Everything works, and I can connect as many client as I want, But I have this problem, when every a spaceship on a client side moves it sends its x,y coords to the server then I loop through the array of sockets and send the same data to every client. But it lags like hell when I do that. What should I do instead? Sorry for my crappy english.... :) Hope you understand.

Share this post


Link to post
Share on other sites
First, read the Forum FAQ. I think the issue of the TCP_NODELAY socket option is mentioned there. Make sure this option is on!

Second, make sure you don't flood your network or machines with too much data. Make sure the client doesn't send more than, say, 50 packets per second. That's assuming few clients and very small packets -- for larger packets and slower connections, you might want a send rate as low as 10 times per second.

Third, put time stamps in your packets, and compare the echo you get back from the server with the local time stamp, to get a measurement of round-trip time. If this is more than, say, 10 milliseconds on a LAN, you're doing something wrong, and need to track it down and fix it.

Fourth, run a profiler on your server and client applications, to make sure you're not spending time in surprising locations.

For a simple LAN game, you can make TCP run just fine, so no need to switch to UDP just yet. Once you have TCP running well on a LAN, with reasonable data rates, then you might want to look into UDP (for reasons explained in the Forum FAQ).

Share this post


Link to post
Share on other sites
...how do I set it to TCP_NODELAY? Should I use setsockopt() ? I don't know how to use it, I don't understand how to use it. Please, help... :)

Share this post


Link to post
Share on other sites
Quote:
Original post by KeLi
But I have this problem, when every a spaceship on a client side moves it sends its x,y coords to the server then I loop through the array of sockets and send the same data to every client. But it lags like hell when I do that. What should I do instead?


it sounds like your sending packets too often. you need to give more information on the game.. is it a top down 2d space shooter? a side scroller? etc..

how do you do movement? is it like this? the user presses an arrow key, and their space ship moves? and when they move, you send there new position to the server?

this sounds to me like your sending packets too much. for example, if they are getting 100 FPS, and hold the arrow key down for 2 seconds, this is 200 packets your sending! you need to put some sort of prediction / interpolation in there and send packets less often, like maybe when they switch directions or something. if you tell more info someone here or i could help you more. good luck!

Share this post


Link to post
Share on other sites
Hi, just looking at it from another angle here. Are you doing any sort of prediction? If your client works like this:

get (x,y) from server
move ship to (x,y)

then it will be choppy (ie, there's no way the server can send that much data 60 times per second or whatever). You have to do something like this:

get(x,y) from server AS WELL AS velocity (u,v)
move ship to (x,y) and set it's velocity to (u,v)
while i'm not getting any more information from the server, move the ship to (x,y)+(u,v)*theTimeThatHasElapsed.


-j

Share this post


Link to post
Share on other sites

This topic is 4755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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