Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can share a socket between two (or more) processes in Linux and even Windows.</p> <p>Under Linux (Or POSIX type OS), using <code>fork()</code> will cause the forked child to have copies of all the parent's file descriptors. Any that it does not close will continue to be shared, and (for example with a TCP listening socket) can be used to <code>accept()</code> new sockets for clients. This is how many servers, including Apache in most cases, work.</p> <p>On Windows the same thing is basically true, except there is no <code>fork()</code> system call so the parent process will need to use <code>CreateProcess</code> or something to create a child process (which can of course use the same executable) and needs to pass it an inheritable handle.</p> <p>Making a listening socket an inheritable handle is not a completely trivial activity but not too tricky either. <code>DuplicateHandle()</code> needs to be used to create a duplicate handle (still in the parent process however), which will have the inheritable flag set on it. Then you can give that handle in the <code>STARTUPINFO</code> structure to the child process in CreateProcess as a <code>STDIN</code>, <code>OUT</code> or <code>ERR</code> handle (assuming you didn't want to use it for anything else).</p> <p>EDIT:</p> <p>Reading the MDSN library , it appears that <code>WSADuplicateSocket</code> is a more robust or correct mechanism of doing this; it is still nontrivial because the parent/child processes need to work out which handle needs to be duplicated by some IPC mechanism (although this could be as simple as a file in the filesystem)</p> <p>CLARIFICATION:</p> <p>In answer to the OP's original question, no, multiple processes cannot <code>bind()</code>; just the original parent process would call <code>bind()</code>, <code>listen()</code> etc, the child processes would just process requests by <code>accept()</code>, <code>send()</code>, <code>recv()</code> etc.</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. 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