Search code examples
network-programmingtcpport

Source port random assignment by windows operating system


I am new to TCP/IP and trying hard to learn basics. When I was using wireshark, I noticed that the source port of my application keeps on changing. Some of my colleagues shared that this is a normal behaviour whereby the operating system will keep changing the source port numbers. Any kind hearted personnel share the reason?

I was expecting that the source port to be fixed at coding level based on my observation at the wireshark.


Solution

  • I was expecting that the source port to be fixed at coding level

    The source port can be explicitly fixed at the coding level using bind(). But this is usually not done, since this would result in unnecessary possible conflicts in the ports used between applications, and also between multiple sockets inside the same application.

    That's why the choice of source port is usually left to the underlying OS, which picks a (more or less random) port from a range of ephemeral ports which are not in use.