简体   繁体   中英

UDP over Internet. How does it work?

As I am programming a network chat (java, but should not make a difference for the question), and wanted to use UDP, I ran into the problem of it not working over the internet. After a little research I found out that you have to have port forwarding for the specific port activated. So now it comes to my question:

Does UDP work over the Internet in a not configurable way?

For example, if I would program a whole Network Game would it make sense to use UDP? Or would I require the Player to activate Portforwarding and open the Port etc?

When would it make sense to use UDP then? And why?

I'm actually not understanding the whole point of UDP then.

For my programming point of view I would like to have a way to use it intuitive. Like creating the DatagramSocket and the DatagramPacket , configure the Packet with the Data and the Destination and send it away over the internet.

As for my Users I don't want them to have to configure any 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.

The problem you've run into is not one of UDP vs TCP (although using the unreliable, unordered UDP as the basis of a chat application seems like an odd choice to me).

The problem is that of NAT traversal . In a nutshell, home routers perform a network function called NAT - Network Address Translation . They do it in order to use a single public IP address for all machines inside the NAT (which are given 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 inside the LAN from the private address to the public one. It uses port numbers to "remember" which machine sent what to what address, in order to perform the backwards translation when the response arrives.

The problem arises when someone wants to a connection to a machine behind a NAT. 与NAT后面的机器的连接时,就会出现问题。 Without seeing an outgoing connection first, the NAT doesn't know to which internal computer and port it should forward the packet. This is what happens to you.

There are various fixes for this issue, with the simplest one being manual port forwarding (as you've discovered), but it's a well known problem faced by any peer-to-peer application. If you need to contact a machine behind NAT (ie contact most home users) and you want your application to work out-of-the box (without your users fiddling with their routers) you need to research NAT traversal techniques, implement them in your application, and hope that the user's home routers support them. It's a huge pain in the neck.

EDITED: with Joachim Pileborg's correct suggestions!

UDP is often a better choice for action-based games, where it's vitally important to have updates to the client or server with the latest data about a player, player input, or the game world.

TCP begins with a 3-way handshake to establish a connection (which takes time). If your game communication protocol is via TCP, all packets in a message have to arrive before the message becomes available. Even a small amount of Internet congestion could cause your game to lag.

TCP is good for communications that MUST arrive in full.

With UDP, the client or server can send the latest player/game state in individual packets that do not depend on arriving in order. If a packet is late, or arrives out of order... it should be ignored.

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

Both should be available in your Java platform.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM