Advertisement

Router choosing random port ?

Started by September 10, 2006 05:25 PM
2 comments, last by md_lasalle 18 years, 5 months ago
Hi all! I'm having some difficulties understanding my routers behaviour... if my udp server bind() to a known port(3333), it receives nicely without problems... BUT when it sends to other, why does the router choose a random port like 61404 insted of using the 3333 that is already bind...and i have a parallele app running (master server) that listens on 11112 for udp packets, and when i send from that app it always using 11112 !!! What makes the router choose to use the binded port or to assign a random one ? Thanks for any help.
the router does masquerading for you (or is it the NAT?). that means it simulates all comps behind him as himself and thus setting other ports. I don't think you can get the router to set the port you want to, but you don't need to. the router saves the address and port where the last packet came from and if somebody uses the port in the udp packet to send you a packet back, the router automatically translates this port for you and sets the original port and sends to your address.

use the address and port given in the udp packet to send packets back and everything should be fine

I hope this helps and isn't to confusing or even wrong ^^ I'm not so sure about the names (masquerading/NAT) but I think the technique is right
Advertisement
What if there were two programs (on two different computers) behind your router, both bound to 3333? Only one of them could be exposed externally on that port, so you have to be NAT clean, and just use the address you get from recvfrom() (passing it back to the response sendto()).

Why your router sometimes picks the same port, and sometimes not, might have to do with the different port ranges; it might have a rule saying "always use a port > 5000 for the outwards facing port, re-using the inner port if it's free" for example.
enum Bool { True, False, FileNotFound };
Thank you for your nice replies, i have now changed the way peers are created and working perfectly, thanks.

This topic is closed to new replies.

Advertisement