How to connect a client and a server?
Even if UDP is connection-less, I would like to establish some kind of connection between a client and a server. The thing is, once the server has received something from the client, the only thing I can do send my answer is use the IP and the Port from the client.
And well... this will, in almost EVERY CASE, not be the IP of the client, because the client doesn't have an IP on the Internet : only its router does. I would like to know how to send back data to the client, even if he's behind a router.
I'm starting to believe it's impossible... but it MUST BE possible : don't tell me videogames use UDP only for sen开发者_开发问答ding data from a client to a server !
Options for UDP behind firewalls:
- Explicit rules on the firewall/NAT forwarding UDP packets to a particular machine.
- UPnP to setup the rules automatically (from the client end)
- Automatic return path for UDP - NAT remembers ports and IP addresses, and creates a return rule for UDP packets.
Just send back to the client through the same socket, the NAT will automagically resolve that and pass it on to the actual box.
Your router will notice that it's rewritten an outgoing data packet to look as if it came from a port on the router, and remember that. If it then receives a UDP packet address to that same port it will say "Oh, I know where to send this!" and forward it on to the port on the inside of the network that original data came from.
usually the router remembers routes for a certain time then forgets them if it doesn't see anything on that port.
精彩评论