7

I've seen many answers similar to this one in regards to serversockets in java: "Let's say you have a server with a serversocket on port 5000. Client A and Client B will be connecting to our server.

Client A sends out a request to the Server on port 5000. The port on Client A's side is chosen by the Operating System. Usually, the OS picks the next port that is available. The starting point for this search is the previously-used port number + 1 (so for instance if the OS happened to us port 45546 recently, the OS would then try 45547).

Assuming there are no connection problems, the Server receives Client A's request to connect on port 5000. The Server then opens up its own next available port, and sends that to the client. Here, Client A connects to the new port, and the server now has port 5000 available again."

I've seen answers like this in multiple questions on stackoverflow about how a different port is used in the returned socket of the accept() than the port that the ServerSocket is listening on. I was always under the impression that TCP is identified by the quartet of information:

Client IP : Client Port and Server IP : Server Port ->protocol too (to distinguish TCP from UDP)

So why would the accept() need to return a socket bound to a different port? Doesn't the quartet of information sent in every header distinguish multiple connections to the same server port from different machines enough where it would not need to use different ports on the server machine for communication?

4
  • Here is a link to the incorrect answer being quoted. If there are any more of these 'many answers similar to this one' please provide links so they can be downvoted and commented on adversely. I don't know why people guess about things like this when the RFC is there to be read.
    – user207421
    Jul 19, 2013 at 0:51
  • There was no guessing. There was a logical progression of information that was based off of a flawed premise. Jul 19, 2013 at 2:33
  • 1
    @RussellUhl An unverified conjecture based on unverified premisses and unexamined logical inferences is essentially guesswork. What was the flawed premiss? When did you consult the RFC? When did you have a look at Socket.getLocalPort(), or a netstat output?
    – user207421
    Jul 19, 2013 at 2:50
  • @EJP: RFC: I didn't, because I was drawing from my classes (which clearly I missed something critical). Socket and netstat: After working with that for half an hour, I came to the conclusion that my answer was incorrect. Only after this realization did I read your comments. In any case, I have since removed my answer. Jul 19, 2013 at 13:09

2 Answers 2

19

The Server then opens up its own next available port, and sends that to the client.

No. It creates a new socket with the same local port number. No second port number is allocated or sent to the client. The SYN/ACK segment which is the server's response to the connect request does not contain a second port number.

Here, Client A connects to the new port,

No. The client acknowledges the SYN/ACK packet and the client is connected to the original port from then on, after acknowledging the SYN/ACK. There is no second connect.

and the server now has port 5000 available again."

It always did.

I've seen answers like this in multiple questions on stackoverflow about how a different port is used in the returned socket of the accept() than the port that the ServerSocket is listening on.

Any such answer is incorrect and should be downvoted 'with extreme prejudice' and commented on adversely. The TCP handshake is defined in RFC 793 and does not specify allocation and exchange of a second port and a second connect message. There are only three messages, which isn't even enough for that to occur.

So why would the accept() need to return a socket bound to a different port?

It doesn't.

Doesn't the quartet of information sent in every header distinguish multiple connections to the same server port from different machines enough where it would not need to use different ports on the server machine for communication?

Yes.

6
  • You're not, but some people are. It doesn't even begin to make sense. What stops this second connection process from starting a third connection and thus becoming an infinite regress? Where in the SYN/ACK packet is this mythical second port? How can two connections be made in three messages? Why is SO full of questions about thousands of port 80 lines in TIME_WAIT in a netstat display? It's completely ridiculous.
    – user207421
    Jul 19, 2013 at 0:51
  • 1
    @EJP +1 for annotating the entire question :). Great answer.
    – Colin M
    Jul 19, 2013 at 2:14
  • I'm not sure what I am referring to anymore. It is entirely possible that I am mistaking TCP with application-level protocols. In any case, I freely acknowledge that my fundamental understanding of TCP is severely flawed. Jul 19, 2013 at 2:32
  • @RussellUhl Application-level protocols can't behave like this either, unless TCP does, or unless they waste resources by creating further listening sockets on ephemeral ports and requiring secondary connects. What would be the point?
    – user207421
    Jul 19, 2013 at 2:54
  • @user207421 - for every new accepted connection on the server side => a socket that is created on the server side, is there also a corresponding client side socket created which is also a 4 tuple <S-IP,S-P,C-IP,C-P> on the client side? May 8, 2019 at 16:10
6

You are correct in the TCP packet header's information. It contains:

Client IP | Client Port | Server IP | Server Port | Protocol

Or, more appropriately (since client/server become confusing when you think about bi-directional transfer):

Source IP | Source Port | Destination IP | Destination Port | Protocol

Multiple connections to the same server port will come from different ports on the client. An example may be:

0.0.0.0:45000 -> 1.1.1.1:80
0.0.0.0:45001 -> 1.1.1.1:80

The difference in client ports is enough to disambiguate the two sockets, and thus have two separate connections. There is no need for the server to open another socket on another port. It does receive a socket from the accept method, but it's assigned to the same port and is now a route back to the newly accepted client.

FTP, on the other hand, does have a model where the server will open a new unprivileged port (> 1023) and send that back to the client for the client to connect to (this is referred to as "Passive FTP"). This is to resolve issues where the client is behind a firewall and can't accept incoming data connections from the server. However, this is not the case in a typical HTTP server (or any other standard socket implementation). It's functionality that is layered on top of FTP.

1
  • 2
    @sunrize920 Yes. A server can accept data from various clients on the same port, so long as the source IP/port is different. A port is nothing but a number.
    – Colin M
    Jul 18, 2013 at 18:57

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.