Home > Blockchain >  Duplicate Windows sockets between unrelated processes
Duplicate Windows sockets between unrelated processes

Time:12-17

I'm using C and Winsock2 for my learning project.

I have some questions that I hope some one can confirm.

Let say I have 2 unrelated processes, process A and process B ( without using CreateProcess ). By unrelated I mean it's not parent and child.

1)

Is it possible in Windows to Accept a socket in process A and pass it to process B if they are unrelated?

2)

I guess i have to use WSADuplicateSocket? but that only works for related processes?

I hope someone can explain and confirm the above..

CodePudding user response:

Is it possible in Windows to Accept a socket in process A and pass it to process B if they are unrelated?

Yes, via WSADuplicateSocket():

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.

I guess i have to use WSADuplicateSocket?

Yes.

but that only works for related processes?

No. It will work fine between any 2 processes, as long as process A knows process B's Process ID, as that is a required parameter of WSADuplicateSocket().

  • Related