I want to know if there is a way to broadcast data through udp sockets for clients behind a NAT. I found many examples where the server is sending data to a multicast address and clients listen on a fixed port. But if there is a NAT between them, the packages need to be sent to different ports, depending on how the NAT has created the port translations right ? So what is the solution here ?
Asked
Active
Viewed 493 times
1
-
A google search for "UDP hole punching" should lead you in the right direction. One possible algorithm is described here: http://en.wikipedia.org/wiki/UDP_hole_punching – atkretsch Nov 20 '13 at 18:03
-
I have read about that but i was wondering if there is a java example somwhere. This technique seams a little more complex (for ex. additional packages need to be transmited to keep alive the "connection") – Andrei F Nov 20 '13 at 19:07
-
Unfortunately I don't know of a Java example offhand. And you're right, it is a more complex approach - but unfortunately, NAT makes it a tougher problem to solve since each client may not (and in fact probably does not) know the port that the NAT assigned. – atkretsch Nov 20 '13 at 19:19
-
I did come across a utility that appears to implement something like the above algorithm - but it's in C#, so there will obviously be some language/library differences. Might still be helpful to look at though. Good luck! https://code.google.com/p/lidgren-network/ – atkretsch Nov 20 '13 at 19:20
-
Readin the article about udp hole punching, i realised i didnt understand something quite well: when the server sends (back) a packet to the client (more exactly to the nat's address), it specifies the port as being the nat's N port (from witch it lastly originate) or is it the client's port on whitch it listens ? – Andrei F Nov 20 '13 at 19:59
-
It would be the port on the NAT, since that's the only port that the server or the other client would ever know about. The NAT hides the actual port used by the client behind it, and translates the pair publicIP:publicPort to privateIP:privatePort. – atkretsch Nov 20 '13 at 20:12
-
Well, that is what i thought too, but this rises another question for me (sry for being noob): How can broadcast be possible (java, in my case), since, when creating a datagram packet, you specify 1 port, and then send it to a multicast address? Doesn't that mean that every NAT will receive packages on the same port number, and will not know what to do with it ? – Andrei F Nov 21 '13 at 06:14
-
Sorry, I lost sight of the fact that you needed multicast, which complicates things quite a bit. Depending on the actual configuration (do you control the NAT? Are all the clients behind the same NAT? etc.) there are probably some things you can do, like tunneling or using TCP to relay across the NAT(s) and then broadcasting. There's some discussion of this sort of problem here: http://stackoverflow.com/questions/3068497/udp-multicast-over-the-internet. – atkretsch Nov 21 '13 at 13:48
-
Thanks, I didn't come across that one... – Andrei F Nov 21 '13 at 15:04