Lot's of Lag, need help.

Started by
6 comments, last by Queasy 19 years, 4 months ago
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.
Advertisement
Its mostlikely due to thread locking. It can use UDP and resend over and over.
--What are you nutz?I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.
What should I do ? should I use UDP?
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).
enum Bool { True, False, FileNotFound };
...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... :)
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!
FTA, my 2D futuristic action MMORPG
1. Go to google
2. Type in "TCP_NODELAY socket sample"
3. The third link that comes up is this
enum Bool { True, False, FileNotFound };
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
Jonathan Makqueasy gamesgate 88[email=jon.mak@utoronto.ca]email[/email]

This topic is closed to new replies.

Advertisement