WSADuplicateSocket

The Windows Sockets WSADuplicateSocket function returns a WSAPROTOCOL_INFO structure that can be used to create a new socket descriptor for a shared socket.

int WSADuplicateSocket (
  SOCKET s,                          
  DWORD dwProcessId,                 
  LPWSAPROTOCOL_INFO lpProtocolInfo  
);
 

Parameters

s
[in] A descriptor identifying the local socket.
dwProcessId
[in] The process ID of the target process in which the duplicated socket will be used.
lpProtocolInfo
[out] A pointer to a buffer allocated by the client that is large enough to contain a WSAPROTOCOL_INFO structure. The service provider copies the protocol info structure contents to this buffer.

Remarks

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.

Sockets can be shared among threads in a given process without using the WSADuplicateSocket function because a socket descriptor is valid in all threads of a process

One possible scenario for establishing and handing off a shared socket is illustrated below:

Source Process IPC Destination Process
1) WSASocket, WSAConnect
2) Request target process ID Þ
3) Receive process ID request and respond
4) Receive process ID Ü
5) Call WSADuplicateSocket to get a special WSAPROTOCOL_INFO structure
6) Send WSAPROTOCOL_INFO structure to target
Þ 7) Receive WSAPROTOCOL_INFO structure
8) Call WSASocket to create shared socket descriptor.
10) closesocket 9)Use shared socket for data exchange

The descriptors that reference a shared socket can be used independently for I/O. However, the Windows Sockets interface does not implement any type of access control so it is up to the processes involved to coordinate their operations on a shared socket. Shared sockets are typically used to have one process that is responsible for creating sockets and establishing connections, and other processes that are responsible for information exchange.

All of the state information associated with a socket is held in common across all the descriptors because the socket descriptors are duplicated and not the actual socket. For example, a setsockopt operation performed using one descriptor is subsequently visible using a getsockopt from any or all descriptors. A process can call closesocket on a duplicated socket and the descriptor will become deallocated. The underlying socket, however, will remain open until closesocket is called by the last remaining descriptor.

Notification on shared sockets is subject to the usual constraints of WSAAsyncSelect and WSAEventSelect. Issuing either of these calls using any of the shared descriptors cancels any previous event registration for the socket, regardless of which descriptor was used to make that registration. Thus, for example, a shared socket cannot deliver FD_READ events to process A and FD_WRITE events to process B. For situations when such tight coordination is required, developers would be advised to use threads instead of separate processes.

Return Values

If no error occurs, WSADuplicateSocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error Codes

WSANOTINITIALISED A successful WSAStartup must occur before using this function.
WSAENETDOWN The network subsystem has failed.
WSAEINVAL Indicates that one of the specified parameters was invalid.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEMFILE No more socket descriptors are available.
WSAENOBUFS No buffer space is available. The socket cannot be created.
WSAENOTSOCK The descriptor is not a socket.
WSAEFAULT The lpProtocolInfo argument is not a valid part of the user address space.

QuickInfo

  Windows NT: Yes
  Windows: Yes
  Windows CE: Unsupported.
  Header: Declared in winsock2.h.
  Import Library: Link with ws2_32.lib.

See Also

WSASocket