UDP over the Internet. How it works?

As I am programming network chat (java, but shouldn't matter to the question), and wanted to use UDP, I ran into the problem that it doesn't work over the internet. After a little research, I found out that you need to forward the port for a specific port. So now my question is:

Does UDP work over the Internet in a non-configurable manner?

For example, if I were to program an entire networked game, would it be wise to use UDP? Or do I need the Player to activate Portforwarding and open the port, etc.?

When would it be advisable to use UDP? And why?

Actually I don't understand the whole point of UDP.

For my programming point of view, I would like to be able to use it intuitively. Like creation DatagramSocket

and DatagramPacket

, customize the data and destination package and submit it over the Internet.

As far as my users are concerned, I don't want them to configure specific things like opening the exact port they want to use, etc. I just want them to use the program (server and client) and it should work.

+3


source to share


2 answers


The problem you are facing is not one of UDP and TCP (although using unreliable, unordered UDP as the basis for a chat application seems like a strange choice to me).

The problem is NAT traversal . In a nutshell, home routers perform the network function NAT - Network Address Translation . They do this in order to use a single public IP address for all machines inside the NAT (which are assigned private addresses - usually 10.0.0.0 or 192.168.0.0). The router then switches the source IP address in all packets sent from the LAN from a private address to a public one. It uses port numbers to "remember" which machine sent to which address so that it can translate back when a reply comes in.



The problem arises when someone wants to initiate a connection to a machine behind NAT. Not seeing the outgoing connection at first, NAT does not know to which internal computer and port it should forward the packet. This is what happens to you.

There are various fixes for this problem, with the simplest being manual port forwarding (as you discovered), but this is a well-known issue that peer-to-peer applications are facing. If you need to communicate with a machine behind NAT (i.e. reach out to most home users) and you want your application to work out of the box (without your users playing with their routers), you need to investigate NAT traversal techniques. implement them in your application, and hope that users' home routers support them. This is a huge pain in the neck.

+4


source


EDITED: with the recommendations of Joachim Pileborg!

UDP is often the best choice for action based games where it is vital to have client or server updates with the latest player, player, or game world data.

TCP starts with a three-way handshake to establish a connection (which takes time). If your game protocol is TCP, all packets in a message must arrive before the message is available. Even a small amount of internet congestion can delay your game.

TCP is good for messages that MUST arrive in full.



In UDP, a client or server can send the latest player / game state in separate packets that are independent of the incoming one in order. If a packet is delayed or out of order ... it should be ignored.

UDP is good for communications that need to be fast, but losing individual packets is fine.

Both must be available on your Java platform.

Here's another reading: http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/

0


source







All Articles