Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes - you need one socket for each connection.</p> <p>A socket is a client IP address + client port + server IP address + server port combination. If a client is talking to multiple servers, it is using multiple ports on the client machine.</p> <p>Each time you connect() a socket, you are allocating a new port.</p> <p>You can specify what client port you want to use by bind()-ing to the port number. Otherwise, the operating system chooses a port for you. This is called an "ephemeral" port.</p> <p>If a server is talking to multiple clients, there is a socket created for each client by the call to accept().</p> <p>An analogy that I remember:</p> <p>Imagine that you have an apartment building with many people living in it. There are mailboxes available out front for the people in the apartment to use.</p> <p>The apartment building corresponds to a computer, the people in the apartment correspond to programs running on the computer, and the mailboxes are ports. Each port can be used by only one application at a time as its private in/out box.</p> <p>A socket is a link between a single mailbox on one building to a single mailbox on another. It is the "to" and "from" address. The socket API uses a socket to know where to send packets, and what reply address to put on the packet. A socket may even link two mailboxes on the same building.</p> <p>A single application on the computer may be using many ports, and they may even be connected to the same port on the same remote computer. Therefore the socket information (IP address + port on <em>both</em> connected machines) is needed to eliminate any ambiguity over the destination and return addresses.</p> <p>Likewise multiple applications on a single computer may be connected to the same port on the same server and sending requests, but the server sends its response to the right application because the return addresses have different port numbers.</p> <p>A socket has the same 4 pieces of information on both communicating machines.</p> <p>In client/server communication for a Web server, many clients are connected to one server port. So the sockets all look like { client(n) IP, client(n) port, server IP, server port 80 }.</p> <p>As @DavidGelhar said it would theoretically be possible for multiple servers to be communicating with the same port on the client, because the socket information specifies the correct server address to send to. However you cannot connect() a single port multiple times. The servers could all connect() to that same client port, but then they can no longer be called servers :)</p> <p>Basically, a socket is just a 4-piece data structure that the socket API uses to know where to send data to, and what return address to put on the data. Likewise a "port" is just a number used as an address to target a specific application on a machine, it is by no means an actual hardware object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload