Hi,
Any body knows how is the port number bound with a socket in detail and how is the port used to forward the packet received in transport layer to a socket which is reading on this port?
thanks.
Hi,
Any body knows how is the port number bound with a socket in detail and how is the port used to forward the packet received in transport layer to a socket which is reading on this port?
thanks.
The application binds to a local IP address and port using the bind()
function. The remote IP address and port is determined by the other end of the connection at the time a connection is established.
In the kernel, at the time a tcp connection is established the socket is put into a hash table based on data including the local address, local port, remote address, and remote port. When an incoming tcp segment arrives, these values are extracted from the header and used to look up the corresponding socket in the hash table. In Linux this lookup occurs in the function inet_lookup_established()
. A similar function, inet_lookup_listener()
is used to look up a listening socket from a different hash table for a new connection; in that case the remote IP address and port are not used.