Winsock Lag (TCP)

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Winsock Lag (TCP)

Post by LunaRebirth »

Hello!

First, I need to clarify that this lag problem is not because of Irrlicht. Winsock is lagging up my program.

I've created an online multiplayer program using Winsock with Irrlicht. Made a server and client for this. When running the server and client on the same PC, everything is perfect and running very smoothly!
But if I have a friend host the server, my client lags TONS. Less than half the normal FPS.

So my questions is.. Would anyone happen to know how to reduce/get rid of this problem?
The lag being client-sided shouldn't happen. I understand server-side lag where other players don't update, but client lag is still happening.

-I'm using ioctlsockets to stop from blocking on Recv.
-I'm checking Recv with sizes using: if ((size = recv(...) > 0)) { ... }
-Unless a client moves, (Presses up,down,left, or right) they do not send data to the server.
-Server does not send your own data to yourself

(It's a multi-threaded server, and the client runs on one thread upon startup -- for sending/recving)

I've followed many steps from multiple tutorials and winsock sites, but no matter what; The clientside lag is still a huge problem!

Any help would be greatly appreciated, If any code snippets would be useful.. Please let me know. Wasn't sure what needed to be shown here.
Thanks!!!

P.S. I don't want to download anything extra like RakNet or Enet, Winsock is free.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Winsock Lag (TCP)

Post by hendu »

Well, what exactly is slow? Add timing calls to your main loop, use binary search to halve the area each time. Can't really give any advice without that info.
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Re: Winsock Lag (TCP)

Post by LunaRebirth »

I don't understand what you mean :o

By slow I mean, If I was to turn my screen in a full 360d angle while I was the one hosting the server, it may take 3 seconds.
If I have a friend host the server, it will take 10+ seconds to fully rotate.
And if I remove the server calls, making it an offline game, it's normal (Like hosting the server and playing client on the same computer). It'd definitely Winosck making the clientside lag
Ruxify
Posts: 33
Joined: Tue Oct 16, 2012 12:37 am

Re: Winsock Lag (TCP)

Post by Ruxify »

Well, first of all you probably should use time-steps so your movements aren't frame dependent. (Just multiply all your speeds for moving/rotation etc with how long it took to loop through one frame in seconds using the irrlichtdevice->getTimer()->getTime(), in case you didn't know that). This should make all movements stable and the same EXCEPT you'll still have frame-rate issues, of which, I'm not sure what it could possibly be.

My first idea was that recv() was blocking, but then I read that you turned off blocking. It's quite odd seeming as I've used winsock in some of my tests and demos and it's all worked perfectly fine.
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Re: Winsock Lag (TCP)

Post by LunaRebirth »

Well, even if I don't do coordinates or rotations being sent/recvd.
If it's something simple like

Code: Select all

int RecvTheNumberOne;
recv(sock,(char*)&RecvTheNumberOne,sizeof(int),NULL);
It still really slows the program down for anyone who is not the host.

I tried doing like Tutorial 4 and making everything update by timer (in case of low FPS) but for anyone who is not on the same computer as the server bugs out (Like walking really quick and then stopping multiple times per second) which is quite annoying.

Edit:
Sorry Ruxify, just re-read your reply and I didn't understand correctly. Paragraph 1 was off topic, but paragraph 2 should answer that I did try that. (But again, it's buggy so I'm trying to stay away from it for now)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Winsock Lag (TCP)

Post by mongoose7 »

You may be sending huge amounts of data, which wouldn't be so noticeable locally. You may also be Nagled to death. Try to send less data. Have the client anticipate as much as possible and have the server confirm.
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Re: Winsock Lag (TCP)

Post by LunaRebirth »

I got it worked out.
Changed the camera to also update to node position to timeframe. (Using Tutorial4's method) and the character looks like he moves quickly and smoothly (Even with low FPS).
It worked, and so it looks normal now for everyone.

Also, how much is "huge amounts of data"? Because I'm sending (double)PlayerX,Y,Z (double)PlayerRotationX,Y,Z and (double)PlayerID (7 doubles total)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Winsock Lag (TCP)

Post by mongoose7 »

Do you send the data in one packet? If you send it in three packets you will be hit by the Nagle algorithm. Actually, you should only call send() once per frame (or less).

Also, you can check the network I/O and CPU utilisation on the server.
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Winsock Lag (TCP)

Post by thanhle »

7 double is not much.
When you get slowdown. Maybe you can use the prediction method so that the motion of the character is continuous.
E.g. If the character move in a certain direction then send the direction once, then use prediction method on clientside to predict where the character is on the next frame or so. Overwrite prediction with actual when other character change direction etc. That will likely optimise the packet transfer rate.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Winsock Lag (TCP)

Post by REDDemon »

I assume your socket is blocking(waiting response packet: on my connection that will take hundreds milliseconds) and you need it to be unblocking (return immediatly, you have to poll it everyframe)?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply