7

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.

Malt
  • 28,965
  • 9
  • 65
  • 105
Loki
  • 4,065
  • 4
  • 29
  • 51
  • 1
    yesterday was more specific to the project. And I did not get an answer why the question kind of is in the depths of StackOverflow now. As I want to have the answer, I'm asking again – Loki Aug 02 '14 at 13:23
  • Most multiplayer games uses UDP for the bulk of their communication. But not often for chat though, then they use TCP. – Some programmer dude Aug 02 '14 at 13:23
  • Don't ask the same (or too similar) question twice, instead modify the existing question. And be patient, you might not get an answer immediately, but good question tend to get answers sooner or later. – Some programmer dude Aug 02 '14 at 13:25
  • 1
    So how do they handle the requirement for the user to open the port and activate port forwarding? – Loki Aug 02 '14 at 13:25
  • 1
    You might want to read and learn about [UPnP](http://en.wikipedia.org/wiki/UPnP). – Some programmer dude Aug 02 '14 at 13:29

2 Answers2

6

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 initiate a connection to a machine behind a 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 (i.e. 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.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
Malt
  • 28,965
  • 9
  • 65
  • 105
  • So, getting back to the game example, they use NAT traversal techniques in order to keep the configuration process of the users? – Loki Aug 02 '14 at 13:58
  • 1
    Peer to peer applications (such as bit torrent, skype etc) all have to do some form of NAT Traversal. An example technique for traversing a NAT is having the users keep a permanent TCP connection (initiated by the users) to some central server. Then if someone wants to message a specific user, the message is sent to the server, which relays it to the relevant user. – Malt Aug 02 '14 at 14:01
  • 1
    Another example is using one of several protocols for automatically configuring the NAT. For example IGD: https://en.wikipedia.org/wiki/Internet_Gateway_Device_Protocol The idea is that you use IGD in user program to configure the user's NAT. – Malt Aug 02 '14 at 14:03
  • Do I have to do this with TCP also? – Loki Aug 02 '14 at 14:04
  • 1
    Yeah, it's pretty much the same problem with TCP, as long as the machine listening for an incoming connection is behind NAT. Here, look at the NAT guide on Vuze's (bit torrent client) wiki: http://wiki.vuze.com/w/NAT_problem – Malt Aug 02 '14 at 14:41
  • 1
    Here's another potentially useful link: http://stackoverflow.com/questions/1539339/how-does-skype-work-without-port-forwarding – Malt Aug 02 '14 at 14:43
  • Well, thanks for all the info. I'm not sure if it will fix my problem soon, but it looks like you got me on the right track. – Loki Aug 02 '14 at 14:44
0

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/

peterg
  • 13
  • 3
  • 1
    TCP/IP is the common name of the complete IP stack, I think you mean just TCP. And it's only synchronous when it comes to connection establishment, once connection is established the protocol is completely asynchronous. – Some programmer dude Aug 02 '14 at 13:32