Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Mar 4 '10 at 4:47

This question came from our site for professional and enthusiast programmers.

6 Answers 6

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.

share|improve this answer

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.

share|improve this answer
    
I believe the question was about the source port, not destination. The source port does increment. –  grawity Mar 4 '10 at 14:08

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.