Socket.DuplicateAndClose(Int32) Method

Definition

Duplicates the socket reference for the target process, and closes the socket for this process.

public:
 System::Net::Sockets::SocketInformation DuplicateAndClose(int targetProcessId);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public System.Net.Sockets.SocketInformation DuplicateAndClose (int targetProcessId);
public System.Net.Sockets.SocketInformation DuplicateAndClose (int targetProcessId);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.DuplicateAndClose : int -> System.Net.Sockets.SocketInformation
member this.DuplicateAndClose : int -> System.Net.Sockets.SocketInformation
Public Function DuplicateAndClose (targetProcessId As Integer) As SocketInformation

Parameters

targetProcessId
Int32

The ID of the target process where a duplicate of the socket reference is created.

Returns

The socket reference to be passed to the target process.

Attributes

Exceptions

.NET Core on any OS and .NET 5+ on Unix-based OS only: The current platform is not supported.

targetProcessID is not a valid process id.

-or-

Duplication of the socket reference failed.

Remarks

The target process should use the Socket(SocketInformation) constructor to create the duplicate socket instance.

Do not call the Socket(SocketInformation) constructor multiple times using the same byte array in the SocketInformation argument in each call. If you do, you'll have multiple managed Socket instances with the same underlying socket, which is strongly discouraged.

On .NET Framework, if the process creating the socket uses asynchronous methods, the process must first set the UseOnlyOverlappedIO property to true. Otherwise, the asynchronous method will bind the socket to an I/O completion port of the creating process, which may cause an ArgumentNullException to be thrown in the target process.

On .NET Core 1.0-3.1, this method is unsupported.

On .NET 5+, DuplicateAndClose(Int32) has limited support on Windows. Unlike on .NET Framework, the UseOnlyOverlappedIO property is a NOP, therefore the process creating the socket must never call asynchronous methods on the socket. A call to an asynchronous operation will always bind it to an I/O completion port of the creating process, which may cause an ArgumentNullException to be thrown in the target process.

Applies to