2

as i have known each tcp connection has a certain port number(destination and source)...

given when one requests to the server, the sender side uses the source port number, and with multiple requests this port number increments..(correct me if im wrong with this)

my question is in what point that this number resets or is it always unique?

0

6 Answers 6

1

This does not happen. The port should stay consistent for the duration of the session. Even calls to accept that retrieve new connections will wind up on the same port.

1

The port number does not increment on requests/responses. A port number is sort of like a telephone extension... Your computer makes a request to an IP address (the phone number) and a specific port (the extension). The port number tells the server on the receiving end which application this request belongs to (a la port number).

Thus, a port number remains consistent so a client application can get in touch with a server application. If these numbers changed, the client would have no idea which "extension" to send the request to on the server end... causing the request to be invalid or get lost.

1
  • I believe the question was about the source port, not destination. The source port does increment. Mar 4, 2010 at 14:08
1

The port on the client is arbitrary and is referred to as ephemeral, for your reference.

1

Your source port gets assigned by the operating system - unless the software attempted to request a specific source port - and typically operating systems use the top 30k or so ports for source addresses. So if you make tens of thousands of requests, eventually you have to recycle your source port.

Many operating systems simply attempt to increment their last source port until they find an available one, whereas others draw randomly from the available ports.

1

The source port will change with new TCP sessions, but not within a session. What does change within a sessions is the sequence number. On some operating systems the both of these can be initiated at a predictable number, on others it is random.

I am not up to date, but with Linux SELinux would enable the facility to randomize the source port and initial sequence number (This could be standard by now).

I am guessing the underlying issue is security, particularly session hijacking. If you really want to protect this level of communication you should investigate the various forms if IPSec and VPN Tunnels.

0

The inbound port on the server is referred to as the listening port.

On some server applications the connection is made and held on the listening port, in this case the server will accept no new connection until the connection is closed.

In other cases the connection is initiated on the listening port and then handed off to another port leaving the listening port open for new connections.

The ultimate port number is largely irrelevant unless there is specific needs based on firewalls, security or the like.

You must log in to answer this question.