socket programming - why web server still using listen port 80 to communicate with client even after they accepted the connection?

前端 未结 5 661
渐次进展
渐次进展 2021-01-21 05:13

Usually a web server is listening to any incoming connection through port 80. So, my question is that shouldn\'t it be that in general concept of socket programming is that port

相关标签:
5条回答
  • 2021-01-21 05:38

    Ports are a virtual concept, not a hardware ressource, it's no harder to handle 10 000 connection on 1 port than 1 connection each on 10 000 port (it's probably much faster even)

    0 讨论(0)
  • 2021-01-21 05:42

    shouldn't it be that in general concept of socket programming is that port 80 is for listen for incoming connection. But then after the server accepted the connection, it will use another port e.g port 12345 to communicate with the client.

    No.

    But, when I look into the wireshark, the server is always using port 80 during the communication.

    Yes.

    I am confused here.

    Only because your 'general concept' isn't correct. An accepted socket uses the same local port as the listening socket.

    So what if https://www.facebook.com:443, it has hundreds of thousands of connection to the it at a second. Is it possible for a single port to handle such a large amount of traffic?

    A port is only a number. It isn't a physical thing. It isn't handling anything. TCP is identifying connections based on the tuple {source IP, source port, target IP, target port}. There's no problem as long as the entire tuple is unique.

    0 讨论(0)
  • 2021-01-21 05:46

    When a server listening socket accepts a TCP request in the first time ,the function such as Socket java.net.ServerSocket.accept() will return a new communication socket whoes port number is the same as the port from java.net.ServerSocket.ServerSocket(int port). Here are the screen shots.

    0 讨论(0)
  • 2021-01-21 05:47

    Not all servers are web servers listening on port 80, nor do all servers maintain lasting connections. Web servers in particular are stateless.

    Your suggestion to open a new port for further communication is exactly what happens when using the FTP protocol, but as you have seen this is not necessary.

    Ports are not a physical concept, they exist in a standardised form to allow multiple servers to be reachable on the same host without specialised multiplexing software. Such software does still exist, but for entirely different reasons (see: sshttp). What you see as a response from the server on port 80, the server sees as a reply to you on a not-so-random port the OS assigned your connection.

    0 讨论(0)
  • 2021-01-21 05:48

    A particular socket is uniquely identified by a 5-tuple (i.e. a list of 5 particular properties.) Those properties are:

    1. Source IP Address
    2. Destination IP Address
    3. Source Port Number
    4. Destination Port Number
    5. Transport Protocol (usually TCP or UDP)

    These parameters must be unique for sockets that are open at the same time. Where you're probably getting confused here is what happens on the client side vs. what happens on the server side in TCP. Regardless of the application protocol in question (HTTP, FTP, SMTP, whatever,) TCP behaves the same way.

    When you open a socket on the client side, it will select a random high-number port for the new outgoing connection. This is required, otherwise you would be unable to open two separate sockets on the same computer to the same server. Since it's entirely reasonable to want to do that (and it's very common in the case of web servers, such as having stackoverflow.com open in two separate tabs) and the 5-tuple for each socket must be unique, a random high-number port is used as the source port. However, each of those sockets will connect to port 80 at stackoverflow.com's webserver.

    On the server side of things, stackoverflow.com can already distinguish between those two different sockets from your client, again, because they already have different client-side port numbers. When it sees an incoming request packet from your browser, it knows which of the sockets it has open with you to respond to because of the different source port number. Similarly, when it wants to send a response packet to you, it can send it to the correct endpoint on your side by setting the destination port number to the client-side port number it got the request from.

    The bottom line is that it's unnecessary for each client connection to have a separate port number on the server's side because the server can already uniquely identify each client connection by its client IP address and client-side port number. This is the way TCP (and UDP) sockets work regardless of application-layer protocol.

    0 讨论(0)
提交回复
热议问题