3

I am attempting to create a server and client that utilizes both TCP and UDP. The server works very well in a LAN setting but the UDP messages are not being received when transmitted over a WAN. I believe it is because the UDP socket used to send the data is not remaining in the NAT tables long enough to return any information. Is there a way to either make the UDP port stay open in the router (without port forwarding) or use the same port for UDP as the already connected TCP connection? Thanks in advance.

Stephen
  • 174
  • 4
  • 14

1 Answers1

3

If you're not getting any traffic it is probably simply blocked by the firewall. In this case it is not about forwarding, it is about opening the port.

Most (if not all) NAT/Firewall devices will allow UDP traffic in both directions once a hole is punched through the NAT. That is, if my laptop here, sitting behind a NAT/firewall, sends a UDP packet out to the Internet my NAT/firewall will allow return UDP traffic to the originating port number through. I work a lot with UDP and my experience is that this is the rule and very few exceptions.

Keep in mind though UDP packets are not guaranteed to be delivered.

Is your client behind a NAT? Do any packets the client send get to the server? Is the problem in the server to client direction?

If you use the same port number for UDP and TCP this will not change the situation. You can't piggyback on a TCP connection because it is a different protocol.

Network Address Translation (NAT) Behavioral Requirements for Unicast UDP

http://en.wikipedia.org/wiki/UDP_hole_punching

Community
  • 1
  • 1
Guy Sirton
  • 8,331
  • 2
  • 26
  • 36
  • Yes the problem is in the server to client direction. I believe the problem could lie in what you said. I am not currently using the port that the UDP transmission was originally sent from thus I am unable to return through the NAT. I will attempt to adjust both the client and server to accommodate this. Thanks for the answer. – Stephen Aug 11 '11 at 14:43
  • I attempted what you suggested and have still been unsuccessful. I have the server retrieving the WAN IP and the port sent by the user on connection and then the client is listening on all ports for a communication from the server. Would it be possible for you to take a look at some code and see if there is any glaring mistake? I appreciate it. – Stephen Aug 11 '11 at 20:35
  • @Stephen . Sure. Add a link to your question. The way I'd do it is using recvfrom() and sendto() on the server side. You bind that socket to a specific port number. On the client side make sure you use a single socket. – Guy Sirton Aug 11 '11 at 21:41
  • Here is a link to a question that has all of my communication and connection code. Basically the idea behind my server is that a user makes a TCP connection to login and then all subsequent communications are done through UDP. Thanks for taking a look. http://stackoverflow.com/questions/7018796/tcp-udp-socket-server-on-wan – Stephen Aug 12 '11 at 13:53
  • 1
    @Stephen - I don't see where the client is sending any UDP messages in your code. You probably want to create a client UDP socket. Send something to it. And then receive on that very same socket object (possibly on another thread). Also you're doing trans.ReceiveFrom(content,ref Remote); but I don't see where you are doign SendTo back to the Remote endpoint which is what the server needs to do. – Guy Sirton Aug 12 '11 at 17:36
  • The SendTo message was the final part of that post. I did not realize that it had to do a SendTo back to the Remote instead of an endpoint that contained the same information. I will try that now thanks. – Stephen Aug 12 '11 at 20:00
  • 1
    @Stephen You need to use the remote endpoint because of the NAT. You don't actually know on the server side what the peer port number is. The NAT translates it. – Guy Sirton Aug 12 '11 at 20:35
  • You sir deserve a thousand UP votes! Thanks to you I finally have a working server. Thank you very much! – Stephen Aug 12 '11 at 20:38