How does Port Number really work in TCP?

前端 未结 3 1088
别那么骄傲
别那么骄傲 2020-12-12 13:59

https://serverfault.com/questions/296603/understanding-ports-how-do-multiple-browser-tabs-communicate-at-the-same-time

how can an application use port 80/HTTP withou

3条回答
  •  时光取名叫无心
    2020-12-12 14:34

    Taking your questions in turn:

    A connection is defined by:

    { protocol, local IP, local port, remote IP, remote port }

    (It's better to say local and remote rather than source and destination, because the local port is the source when you send, but the destination when you receive.)

    The sockid is just a descriptor in the user process that maps to the connection in the kernel, just as a file descriptor maps to a file on disk that has been opened.

    Two different processes cannot bind to the same local port. However, it's possible for two processes to use the same connection -- a socket descriptor can be inherited from a parent process to a child process, or the descriptor may be passed between processes using interprocess communications. The two processes would be using the same ports because they're actually sharing the same connection.

    While the protocol allows use of the same local port when connecting to different remote servers or ports, most TCP stacks will not allow this. The API for binding a local port is the same whether you're using it for an outgoing connection or listening for an incoming connection, and the intention isn't specified until AFTER the port is bound. Since only one socket can listen on a particular port, the API simply refuses to allow multiple sockets to bind to a port (there's a special exception to this, but it's not relevant to this discussion). As a result, all outgoing connections will use different local ports. So when the browser opens multiple connections (either to the same or different web servers) they will have different local ports.

提交回复
热议问题