Hello, I would like to know whether a server application will always answer on the same port? I have read somewhere that in order to keep listening on the "listening" port, the server will use different port. Could anybody provide me with more details? Thanks!
As a part of the TCP handshaking process, a communication channel is established on another port. The source and destination 'port' numbers correspond to 16 bit fields in the TCP packet header. Once the channel is established, the communication takes place on that port, with the appropriate source and destination port values in the fields on the header.
The initial connection is made to the listening port on the server; after this, the process establishes a TCP connection on the new ports assigned and further communication takes place on those ports.
If you make a TCP connection to a server on a particular port, that TCP connection will continue to use the same port on the server side.
A TCP connection is established between an (address1, port1)
pair on one side and an (address2, port2)
pair on the other side - and all four of these values are fixed over the life of the TCP connection.
This does not stop the server from listening on the port after it has accepted a connection - because it can differentiate between any packets for the established connection(s) and new connections based on the (address, port)
pair used by the client.
For example, this netstat
output from an Oracle server shows it listening on port 1521, and several established connections also using that port:
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:1521 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:1521 127.0.0.1:32776 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32798 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32823 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32822 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32821 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32820 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32819 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:32818 ESTABLISHED
tcp 0 0 127.0.0.1:1521 127.0.0.1:30536 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:36969 ESTABLISHED
tcp 0 0 127.0.0.1:32776 127.0.0.1:1521 ESTABLISHED
tcp 0 0 127.0.0.1:30536 127.0.0.1:1521 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:37786 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:37035 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:37034 ESTABLISHED
tcp 0 0 192.168.9.126:1521 192.168.9.124:33018 ESTABLISHED