0
votes

The game im developing is currently is using ServerSocket and Socket to provide the multiplayer. It laggs alot when it's not run localy, so im wondering if I should remake the multiplayer structure to send both through TCP (ServerSocket, Socket) and UDP (DatagramSocket). Currently as mentioned im only using TCP and it's only sending around 60 packets a second (30 recieve and 30 sent) when not ran localy.

Information that is sent between server and client:
Movement, trading, ItemUsage, EnvironmentDetails and more stuff.

So my questions are:

Would it make a huge difference if I both used TCP and UDP instead of only TCP?

If I switched to both TCP and UDP what should I send in each protocol?

Thanks in advance!

Edit:

Client:

Instancing: this.out = new ObjectOutputStream(new BufferedOutputStream(this.requestSocket.getOutputStream())); this.in = new ObjectInputStream(new BufferedInputStream(this.requestSocket.getInputStream()));

Sending:

this.out.writeObject(temp.toString()); this.out.flush(); Temp is a JSONObject

Server:

Instancing: this.out = new ObjectOutputStream(new BufferedOutputStream(c.getOutputStream())); this.out.flush(); this.in = new ObjectInputStream(new BufferedInputStream(c.getInputStream()));

Sending: this.out.writeObject(temp.toString()); this.out.flush(); Temp is a JSONObject

1
UDP may drop packets, so you may lose data. Will that negatively affect your game? I could see it potentially being an issue.Carcigenicate
Well, UDP doesn't feature error checking per se, so it's bound to be faster in theory, as long as you don't need an accurate information flow.Mena
It's also possible that it's lagging because of your code, depending on how your communication code is handling things. Do you flush your streams? In what format are you sending data, etc.Kayaman
If you are sending very small packets of data, you may be needing the TCP_NODELAY option to be set. This should send data immediately and not wait the network buffer to fill up, but send the data immediately.Balazs Vago
Very good resource to understand this better: gafferongames.com/post/udp_vs_tcp I also recommend reading all of his articles, since they are nicely put and relevant for game networking.Dejan Pekter

1 Answers

0
votes

Would it make a huge difference if I both used TCP and UDP instead of only TCP?

Yes. You will notice much better performance if you use UDP to update the game's state, because data verification does not occur. The UDP packet is fired and then forgotten.

Also, to increase performance, limit the amount of data that you are sending. If you are sending JSON, every character is one full byte. 30 updates a second might be excessive, because that is an update every 33.3 milliseconds. To put that into perspective, a 60 Hz monitor will only update every 16.7 milliseconds.

If I switched to both TCP and UDP what should I send in each protocol?

TCP is typically used when you need to make sure your data gets to the server or client. UDP is used when you want speed, and can potentially deal with dropped packets. If you use UDP, you will need to account for the potential packet loss in your code.