Does ServerSocket accept return socket on arbitrary port?

后端 未结 2 607
慢半拍i
慢半拍i 2020-11-28 10:44

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

2条回答
  •  醉梦人生
    2020-11-28 11:39

    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.

提交回复
热议问题