Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Important:</strong></p> <p>I'm sorry to say that the response from "Borealid" is imprecise and somewhat incorrect - firstly there is no relation to statefulness or statelessness to answer this question, and most importantly the definition of the tuple for a socket is incorrect. </p> <p><strong>First remember below two rules:</strong></p> <ol> <li><p>Primary key of a socket: A socket is identified by <code>{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}</code> not by <code>{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT}</code> - Protocol is an important part of a socket's definition.</p></li> <li><p>OS Process &amp; Socket mapping: A process can be associated with (can open/can listen to) multiple sockets which might be obvious to many readers.</p></li> </ol> <p><strong>Example 1:</strong> Two clients connecting to same server port means: <code>socket1 {SRC-A, 100, DEST-X,80, TCP}</code> and <code>socket2{SRC-B, 100, DEST-X,80, TCP}</code>. This means host A connects to server X's port 80 and another host B also connects to same server X to the same port 80. Now, how the server handles these two sockets depends on if the server is single threaded or multiple threaded (I'll explain this later). What is important is that one server can listen to multiple sockets simultaneously.</p> <p><strong>To answer the original question of the post:</strong></p> <p>Irrespective of stateful or stateless protocols, two clients can connect to same server port because for each client we can assign a different socket (as client IP will definitely differ). Same client can also have two sockets connecting to same server port - since such sockets differ by <code>SRC-PORT</code>. With all fairness, "Borealid" essentially mentioned the same correct answer but the reference to state-less/full was kind of unnecessary/confusing.</p> <p>To answer the second part of the question on how a server knows which socket to answer. First understand that for a single server process that is listening to same port, there could be more than one sockets (may be from same client or from different clients). Now as long as a server knows which request is associated with which socket, it can always respond to appropriate client using the same socket. Thus a server never needs to open another port in its own node than the original one on which client initially tried to <s>bind</s> connect. If any server allocates different server-ports after a socket is bound, then in my opinion the server is wasting its resource and it must be needing the client to <s>bind</s> connect again to the new port assigned.</p> <p><strong>A bit more for completeness:</strong></p> <p><strong>Example 2:</strong> It's very interesting question that can a server's two different processes listen to same port. If you do not consider protocol as one of parameter defining socket then the answer is no. Initiatively this is so because we can say that in such case for a single client trying to connect to a server-port will not have any mechanism to mention which of the two listening process the client intends to. This is the same theme asserted by rule (2). However this is WRONG answer because 'protocol' is also a part of the socket definition. Thus two processes in same node can listen to same port only if they are using different protocol. For example two unrelated clients (say one is using TCP and another is using UDP) can <s>bind</s> connect and communicate to the same server node and to the same port but they must be served by two different server-processes. </p> <p><strong>Server Types - single &amp; multiple:</strong></p> <p>When a server's processes listening to a port that means multiple sockets can simultaneously connect and communicate with the same server-process. If a server uses only a single child-process to serve all the sockets then the server is called single-process/threaded and if the server uses many sub-processes to serve each socket by one sub-process then the server is called multi-process/threaded server. Note that irrespective of the server's type a server can/should always uses the same initial socket to respond back (no need to allocate another server-port).</p> <p>Suggested <a href="http://rads.stackoverflow.com/amzn/click/0130183806" rel="noreferrer">Books</a> and rest of the two volumes if you can.</p> <p><strong>A Note on Parent/Child Process (in response to query/comment of 'Ioan Alexandru Cucu')</strong></p> <p>Wherever I mentioned any concept in relation to two processes say A and B, consider that they are not related by parent child relationship. OS's (especially UNIX) by design allow a child process to inherit all File-descriptors (FD) from parents. Thus all the sockets (in UNIX like OS are also part of FD) that a process A listening to, can be listened by many more processes A1, A2, .. as long as they are related by parent-child relation to A. But an independent process B (i.e. having no parent-child relation to A) cannot listen to same socket. In addition, also note that this rule of disallowing two independent processes to listen to same socket lies on an OS (or its network libraries) and by far it's obeyed by most OS's. However, one can create own OS which can very well violate this restrictions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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