Socket.SendPacketsAsync(SocketAsyncEventArgs) Method

Definition

Sends a collection of files or in memory data buffers asynchronously to a connected Socket object.

C#
public bool SendPacketsAsync (System.Net.Sockets.SocketAsyncEventArgs e);

Parameters

e
SocketAsyncEventArgs

The SocketAsyncEventArgs object to use for this asynchronous socket operation.

Returns

true if the I/O operation is pending. The Completed event on the e parameter will be raised upon completion of the operation.

false if the I/O operation completed synchronously. In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

Exceptions

The file specified in the FilePath property was not found.

A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

The Socket is not connected to a remote host.

The Socket has been closed.

A connectionless Socket is being used and the file being sent exceeds the maximum packet size of the underlying transport.

Remarks

The SendPacketsAsync method is used to send a collection of files or in memory data buffers to remote host. The Socket must already be connected to the remote host.

If a System.Net.Sockets.SendPacketsElement references a file in the working directory, it may be identified with just the name of the file; otherwise, the full path and name of the file must be specified. Wildcards and UNC share names are supported. If the file is not found, FileNotFoundException is thrown.

To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

The SocketAsyncEventArgs.SendPacketsFlags property on the e parameter provides the Window Sockets service provider with additional information about the file transfer. For more information about how to use this parameter, see TransmitFileOptions.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendPacketsAsync method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

This method uses the TransmitPackets function found in the Windows Sockets 2 API. For more information about the TransmitPackets function and its flags, see the Windows Sockets documentation.

Although intended for connection-oriented protocols, the SendPacketsAsync method also works for connectionless protocols, provided that you first call the BeginConnect, Connect, or ConnectAsync method to establish a default remote host. With connectionless protocols, you must also be sure that the size of your file does not exceed the maximum packet size of the underlying service provider. If it does, the datagram is not sent and SendPacketsAsync throws a SocketException exception.

The SendPacketsAsync method is optimized according to the operating system on which it is used. On Windows server editions, the SendPacketsAsync method is optimized for high performance.

On Windows client editions, the SendPacketsAsync method is optimized for minimum memory and resource utilization.

Use of the TransmitFileOptions.UseKernelApc flag in the SocketAsyncEventArgs.SendPacketsFlags property on the e parameter can deliver significant performance benefits. If the thread initiating the SendPacketsAsync method call is being used for heavy computations, it is possible, though unlikely, that APCs could be prevented from launching. Note that there is a difference between kernel and user-mode APCs. Kernel APCs launch when a thread is in a wait state. User-mode APCs launch when a thread is in an alertable wait state

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

See also