Exactly, UPnP establishes a required tunnel between two endpoints. Mainly used for local ports that are about to receive data (instead of first sending data, where NAT applies).
Normally, with average router hardware
, you shouldn't get problems using the same port on two or more machines behind a router. The router should take care of that in its translation table. When it sees that there're two identical ports used, it should translate one of the two to another port number for packets being send through the internet.
Have you actually tested it? Did it have strange behaviour?
You could do a simple handshake so that a client can make sure that the socket is properly setup:
- Bind your UDP socket.
- Send a packet to the server containing a random number (preferably a hash that's somehow dependant on time and maybe the machine's name to avoid clashes).
- Server answers with the same hash.
- If client receives it below a timeout value, everything's okay, otherwise bind to another port.
Why are you binding your local sockets to specific ports anyway? Normally the OS takes care of choosing the port number so that chances are fairly low that you get a port number another machine on your network already uses. (this, of course, only counts for sockets on the client side, sockets on the server side have to be bind to a specific port, otherwise clients don't know where to send to
)