2

I am working on a feature where we need 2 process running on a system need to send out packets using TCP Sockets to a remote device. Is it possible to share socket handle between 2 Independent processes which are not parent and child on windows C++ ?

My question is similar to this one Socket handle transfer between independent processes but on Windows

amithkrp07
  • 81
  • 1
  • 6
  • This is an [XY Problem](http://xyproblem.info). What problem are you trying to solve? – IInspectable Jun 11 '20 at 11:42
  • You may find the documentation of [WSADuplicateSocket](https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketa) informative. I honestly don't see how doing this fits the rather sketchy description of your post, but it is what it is. – WhozCraig Jun 11 '20 at 11:44
  • Hi @IInspectable, As i said , We have a requirement where 2 independent processes in a host would want to communicate separately to a remote device but using the same port in the HOST. Lets say P1 creates a socket binding to a port and it has to share this socket handle to P2 which uses this handle to talk to remote device. Apparently it seems WSADuplicateSocket seems to be working only with Child and Parent processes not with 2 independent processes. – amithkrp07 Jun 11 '20 at 11:49
  • My question is similar to this one https://stackoverflow.com/questions/12425067/socket-handle-transfer-between-independent-processes?noredirect=1&lq=1 but on Windows. – amithkrp07 Jun 11 '20 at 11:52
  • No, this is not possible. Even if it were, how would you distinguish which parts of a package came from which client on the server side? – 500 - Internal Server Error Jun 11 '20 at 12:00
  • No Need to differentiate , as it is coming from same Host. Server need not differentiate. – amithkrp07 Jun 11 '20 at 12:02
  • TCP is a bidirectional protocol (even if you only ever send the data in one direction). As such it is impossible to have two copies of a TCP endpoint socket; the OS won't know how to route the reply packets. – rustyx Jun 11 '20 at 12:04
  • 1
    In Windows, the parent process is recorded, but not used for anything. If an API allows you to duplicate a resource handle into a target process, it works with any pair of processes, irrespective of whether any of the processes involved is the parent of the respective other. – IInspectable Jun 11 '20 at 13:42

0 Answers0