Game networking

Advanced game related topics
Fuzzle
New User
New User
Posts: 3
Joined: Fri Jun 11, 2010 2:51 pm

Game networking

Post by Fuzzle »

Hi there,

I got some problems with creating a multiplayer game. My server creates a thread for every connected client which waits until a packet is received and then sends it to all of the other clients. I also added ping. When doing nothing, I got a ping of ~90ms (with a friend hosting the server) but when moving (creating some traffic), ping raises to about 400ms.
I'm only submitting when a key is pressed or released and some control packets every second so traffic should be almost nothing. Then I thought the server would be too slow to send the packets in time but the one who's hosting the server has always a ping of 0ms but he would be affected by that too. I don't have any idea about what's wrong here, so I would be really happy if someone could help me understanding all that networking stuff because it's hard to debug and very frustrating..
I can post or upload code if needed but I think it would be better if someone could explain me how all this is working instead of just correcting my code..
DarkPlayer
Enthusiast
Enthusiast
Posts: 107
Joined: Thu May 06, 2010 11:36 pm

Re: Game networking

Post by DarkPlayer »

Hi,

it is hard to say what is going wrong, because you do not seem to use the standard implementation of the network commands and you dont give any further information about your implementation.

So i just can guess, but it seems for me, that you use TCP sockets (because of the different sockets in the threads), which are based on streams and not on packets. So, it is possible that the operating system combines packages, which you sent after another, to one single packet.
Could it be possible, that you do not parse the received packets correct, if you receive different packages in the same memory block? The idea would be, that the server receives MOVE + PING, parses the Move but misses the ping. Later, the client would interpretate a different PING Answer as a reply to a PING request which was not answered. This would result in a big Ping?

DarkPlayer
Fuzzle
New User
New User
Posts: 3
Joined: Fri Jun 11, 2010 2:51 pm

Re: Game networking

Post by Fuzzle »

Ok, I must admit I'm not as good in programming as you think, I don't know anything about using an other implementation and about TCP sockets. I'm using the standard network library and I'm only talking of "packets" because I always send blocks of 10 bytes (+1 added at server for client identification). I want to learn all that stuff, that's why I posted here..
I think those "packets" are received correctly, the other players don't move smooth anymore etc
So I did something wrong because of my lack of knowledge about threads and networking, I want to learn more so I can do better :)
DarkPlayer
Enthusiast
Enthusiast
Posts: 107
Joined: Thu May 06, 2010 11:36 pm

Re: Game networking

Post by DarkPlayer »

Hi,

first, you should not use Threads for every client, they are not necessary for the standard network implementation and would only mess up everything. The second thing ist, that if you use tcp, there are no "packets". Lets say you send 10 bytes, than it can happen that the server receives 1 byte, 5 byte and 4 bytes. You have to save all the received data till you got a full package and if you receive 15 bytes, you also received 5 bytes of the next package. And you don't need a byte for client identification, you can identify them by their connection ids. Otherwise a player could just fake his ID and control another Player :mrgreen:

DarkPlayer
Fuzzle
New User
New User
Posts: 3
Joined: Fri Jun 11, 2010 2:51 pm

Re: Game networking

Post by Fuzzle »

Yeah, I'm already sending and receiving until my "package" is complete (checking how many bytes are actually received/sent, delay and so on) and the client identification is added at the server (in use of the connection ids). The threads, I will try without them..
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: Game networking

Post by Num3 »

I've build quite a number of servers in purebasic, and i've had no problem connecting up to 3000 clients.

It uses 3 threads and linked lists.

I will not be posting code here, because it's too long or under NDA, but here's the logic i use:

Image

I hope it helps!
sebestien
New User
New User
Posts: 1
Joined: Mon Jun 28, 2010 12:00 pm

Re: Game networking

Post by sebestien »

Try to avoid the tcp sockets. Usually UDP sockets promise better performance for games and basically they use less system resources. And btw UDP really works with packets!
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: Game networking

Post by zefiro_flashparty »

examples :D
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Game networking

Post by RichAlgeni »

First of all, here is a good overview of TCP vs. UDP:

http://codeidol.com/unix/unix-network-p ... ad-of-TCP/

Second, I wholeheartedly disagree about using threads. I would recommend threads when serving multiple clients.

Third, I would only use UDP if running over a LAN, as too many things can go wrong over a WAN. That is unless the data sent is not critical in nature.

What you could do to cut down on the overhead of TCP is to leave the socket open once connected. That means you would have to write code to wait for data on each socket, and that's where separate threads really help. If that is something you might want to do and would like some help, just let me know. I'd be glad to assist you. The folks on this board have really helped me!
Post Reply