2

With TCP you listen to some port (say port 80) and then when an incoming connection pops up, you accept it, establishing the connection over a new port.

Why is this necessary? Why can't you just keep communicating over the original port?

4
  • Only one application can use any port at any one time. That means that if an application binds to port 80, another application that needs TCP will need to use a different port. You are running many applications at the same time, and any that use TCP each need to use a different port.
    – Ron Maupin
    Nov 19, 2017 at 21:40
  • @RonMaupin: Yeah, that wasn't what I was asking though. I was apparently just misunderstanding what is actually happening.
    – user541686
    Nov 19, 2017 at 21:46
  • You should look at RFC 793, Transmission Control Protocol, Section 1.5 Operation, especially the parts about multiplexing and connections.
    – Ron Maupin
    Nov 19, 2017 at 22:18
  • You may be thinking of the diffrence between a app like a web server vs the computer connecting for example. A web page that is http will usually be port 80. I just went to ipchicken.com and the port my browser used on my end was 19129. I refreshed the page and it used 22913. My side changes but the web page stays port 80. Being able to have many "streams of communication" using one ip address is a huge benefit. A web server for example can send data to many "devices" with one ip address on diffrent ports. I picture a hose with 65535 little hoses in it.
    – Fixitrod
    Nov 20, 2017 at 3:35

1 Answer 1

5

The incoming connection is accepted and the socket uses the listening port on the server, not another one. Some protocols are more complicated than that but HTTP (from your question's port 80) isn't.

The linked IEEE/POSIX standard states

The accept() function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket.

(You find the current revision here).

Here, "new socket" refers to an API socket handle that uses the same local TCP port number and effectively the same TCP socket than the one used to create the connection.

4
  • +1 wow I can't believe I missed this. Somewhere I got the impression there was a new port being used but I guess I was wrong. Thanks!
    – user541686
    Nov 19, 2017 at 21:45
  • 2
    One reason for confusion is that the TCP RFC uses a different definition of the term "socket" from the one used in modern network programming. Nov 20, 2017 at 0:03
  • @Zac67 on same port 80 could listen more than one request at the same time? For example if one user generate two http request simultaneously, then server listen both request at the same on same port 80? Am I correct?
    – S. M.
    Apr 17, 2022 at 8:42
  • 1
    @AlokMaity Yes, of course. A listening port can accept any number of simultaneous connections, as long as the partner's IP address or port number differs (and the service can handle the load). Anyone wanting to create different connections just has to use different local port numbers.
    – Zac67
    Apr 17, 2022 at 12:31

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.