Export (0) Print
Expand All
Expand Minimize

WSADuplicateSocket function

The WSADuplicateSocket function returns a WSAPROTOCOL_INFO structure that can be used to create a new socket descriptor for a shared socket. The WSADuplicateSocket function cannot be used on a QOS-enabled socket.

Syntax


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

Parameters

s [in]

Descriptor identifying the local socket.

dwProcessId [in]

Process identifier of the target process in which the duplicated socket will be used.

lpProtocolInfo [out]

Pointer to a buffer, allocated by the client, that is large enough to contain a WSAPROTOCOL_INFO structure. The service provider copies the protocol information structure contents to this buffer.

Return value

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 codeMeaning
WSANOTINITIALISED

A successful WSAStartup call 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 parameter is not a valid part of the user address space.

 

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 in the following table.

Source processIPCDestination process
1) WSASocket, WSAConnect
2) Request target process identifier==>
3) Receive process identifier request and respond
4) Receive process identifier<==
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.
9) Use shared socket for data exchange
10) closesocket <==

 

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 having 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. Both the source process and the destination process should pass the same flags to their respective WSASocket function calls. If the source process uses the socket function to create the socket, the destination process must pass the WSA_FLAG_OVERLAPPED flag to its WSASocket function call. 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, 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.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winsock2.h

Library

Ws2_32.lib

DLL

Ws2_32.dll

Unicode and ANSI names

WSADuplicateSocketW (Unicode) and WSADuplicateSocketA (ANSI)

See also

Winsock Reference
Winsock Functions
WSASocket

 

 

Community Additions

ADD
Show:
© 2014 Microsoft