Page 1 of 1
Game networking
Posted: Fri Jun 18, 2010 3:25 pm
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..
Re: Game networking
Posted: Fri Jun 18, 2010 6:06 pm
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
Re: Game networking
Posted: Fri Jun 18, 2010 7:08 pm
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 :)
Re: Game networking
Posted: Fri Jun 18, 2010 7:22 pm
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
DarkPlayer
Re: Game networking
Posted: Fri Jun 18, 2010 7:48 pm
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..
Re: Game networking
Posted: Sun Jun 20, 2010 1:31 pm
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:
I hope it helps!
Re: Game networking
Posted: Mon Jun 28, 2010 12:37 pm
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!
Re: Game networking
Posted: Mon Nov 14, 2011 2:24 pm
by zefiro_flashparty
examples

Re: Game networking
Posted: Sun Dec 11, 2011 10:24 pm
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!