Results 1 to 4 of 4

Thread: How can I share a winsock socket ID across processes?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,079

    How can I share a winsock socket ID across processes?

    I have 2 processes. One is my listener process called "Multiprocess Web Server.exe". When a connection arrives from a client (a web browser), it runs a second program called "ServerProc.exe"), and passes the socket ID (the value returned from the accept() function in the listener process) to that external server process as a commandline argument. But here's the problem. That external process doesn't recognize the socket ID. This remains a problem even though I made sure to also initialize winsock with WSAStartup in the external process. Is there a way to make a socket ID capable of being handed over to an external process? This wouldn't be an issue with multithreading, as then I could keep all the threads in the same process, but VB6 doesn't work properly with multithreading. Therefore my only option is to use a multi-process technique instead a multi-thread technique.

  2. #2
    New Member
    Join Date
    Nov 2021
    Posts
    11

    Re: How can I share a winsock socket ID across processes?

    Those socket handles are per-process. You can however duplicate it into the other process, and then use some way to notify the second process about it:

    DuplicateHandle(srcProcHandle, sockHandle, destProcHandle, destSockHandle, 0, True, DUPLICATE_SAME_ACCESS)

    destSockHandle is passed as a pointer (by reference). If successful, you now have a handle in destProcHandle under which it is known to the destination process. You could then use SendMessage or whatever to tell the destination process about that new handle, and close it in the source process.

    EDIT: Memory is foggy, sorry. Now that I thought about it some more and a quick google search later, it seems you have to use WSADuplicateSocket for sockets, DuplicateHandle might not work.
    Last edited by Zahl; Nov 24th, 2021 at 06:19 AM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,079

    Re: How can I share a winsock socket ID across processes?

    Quote Originally Posted by Zahl View Post
    Those socket handles are per-process. You can however duplicate it into the other process, and then use some way to notify the second process about it:

    DuplicateHandle(srcProcHandle, sockHandle, destProcHandle, destSockHandle, 0, True, DUPLICATE_SAME_ACCESS)

    destSockHandle is passed as a pointer (by reference). If successful, you now have a handle in destProcHandle under which it is known to the destination process. You could then use SendMessage or whatever to tell the destination process about that new handle, and close it in the source process.

    EDIT: Memory is foggy, sorry. Now that I thought about it some more and a quick google search later, it seems you have to use WSADuplicateSocket for sockets, DuplicateHandle might not work.
    So if I use WSADuplicateSocket to clone the socket for the other process, and also share that info with the other process, how do I load that cloned socket into the other the other process? Is there something like WSALoadDuplicatedSocket that the external process can call?

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,437

    Re: How can I share a winsock socket ID across processes?

    In the Remarks section of WSADuplicateSocket it says

    The WSADuplicateSocket function is used to enable socket sharing between processes. A source process calls WSADuplicateSocket to obtain a special WSAPROTOCOL_INFO structure. It uses some interprocess communications (IPC) mechanism to pass the contents of this structure to a target process, which in turn uses it in a call to WSASocket to obtain a descriptor for the duplicated socket. The special WSAPROTOCOL_INFO structure can only be used once by the target process.
    DuplicateHandle works on hSockets in-process just fine but for out-of-proc duplication WSADuplicateSocket seems to be required indeed.

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width