Do TCP client ports and TCP server ports have the same address space?

On the same computer, if the tcp client has taken port 12345, for example, the client connected to google.com and then the tcp server tries to bind its listening port to 12345, is that allowed?

+3


source to share


2 answers


Answer: "It depends" (OS and socket options).

On Linux with SO_REUSEADDR

both sockets, the exact situation is possible:

$ sudo netstat -panl |grep 12300
tcp        0      0 127.0.0.1:12300         0.0.0.0:*               LISTEN      3591/nc         
tcp        0      0 127.0.0.1:12300         127.0.0.1:25            ESTABLISHED 3547/nc         
tcp        0      0 127.0.0.1:25            127.0.0.1:12300         ESTABLISHED 3548/exim4

      



... but only when the client gets there first. When the server is already listening, the same port cannot be bound by the client (and it will never be automatically assigned to the client, IIRC).

On Windows with or without SO_REUSEADDR

port is a port and is bind

not working (whether it is the server or client that did it in the first place).

+4


source


Not. Port - port.

IT SAID: Collisions are rarely there because usually servers start before the client asks for an undefined port (i.e. the client never sets a port number, his machine takes a free one).

And there are predefined ports for most servers.

http://www.webopedia.com/quick_ref/portnumbers.asp http://www.ietf.org/rfc/rfc1700.txt?number=1700



And the ranges:

http://www.tcpipguide.com/free/t_TCPIPApplicationAssignmentsandServerPortNumberRang-2.htm

As you can see, the registerd ports go to 49151 and from there they are reserved for dynamic use. So client ports usually come from that 49152 range upwards, which services shouldn't be used.

-2


source







All Articles