54

When a client connects to a server using TCP, a new socket is created for the TCP stream. Does the connection remain on the same port the connection was made or does it get changed to some other port?

3 Answers 3

55

The new socket is an application-level concept introduced because each established connection needs a unique file descriptor (also distinct from the listening file descriptor), which maps to, but isn't the same as, a TCP session. The session itself is identified by the combination of source and destination address and port. The source (client) port is usually chosen at random, while the destination (server) port is the listen port. No additional port is allocated.

5
  • 2
    The session is identified by the combination of source and destination ip and port, AND the protocol. Hence it's a 5-tuple that uniquely identifies the connection, not 4-tuple.
    – treecoder
    Jun 9, 2015 at 4:04
  • 1
    @treecoder Which level protocol? Transport level? Or Application level? E.g. <ip1, port1, ip2, port2, tcp> or <ip1, port1, ip2, port2, http>? Nov 11, 2015 at 7:28
  • 4
    @treecoder For TCP, the protocol always 6, and is thus irrelevant for identifying TCP sessions. Jan 10, 2016 at 10:56
  • @smwikipedia Layer 4 (transport). Jan 10, 2016 at 10:57
  • 2
    @KawaiKx No, it's transport layer, and it's irrelevant for session identification purposes, since the protocol is defined to be 6 for all TCP connections. Jan 10, 2016 at 10:59
16

The server use the same port to listen and accept new connection, and communicate to the remote client.

Let's me give you an example, (in linux system):

First, start a http server by python:

xiongyu@ubuntu:~$ sudo python -m SimpleHTTPServer 500
Serving HTTP on 0.0.0.0 port 500 ...

Second use nc command to connect to the http server, here we start two client by:

xiongyu@ubuntu:~$ nc 0.0.0.0 500

Use netstat to see the netstate of port 500:

xiongyu@ubuntu:~$ netstat -natp |grep ':500'
tcp    0      0 0.0.0.0:500         0.0.0.0:*          LISTEN      54661/python
tcp    0      0 127.0.0.1:51586     127.0.0.1:500      ESTABLISHED 57078/nc
tcp    0      0 127.0.0.1:51584     127.0.0.1:500      ESTABLISHED 54542/nc
tcp    0      0 127.0.0.1:500       127.0.0.1:51586    ESTABLISHED -
tcp    0      0 127.0.0.1:500       127.0.0.1:51584    ESTABLISHED 54661/python

You can see, the http server use port 500 to LISTEN for the client, after a new client connected to the server, it still use the port 500 to communite with the client, but with a new file descriptor .

11

The socket associated with the new descriptor returned by accept on the server will use the same port on the server side of the connection as the original socket (assuming "normal" definitions where the client initiates the connection). The new socket will have a different client port number (the remote port from the server's point of view).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.