System.Net.Sockets.Socket.BeginSendFile Method

Sends a file and buffers of data asynchronously to a connected System.Net.Sockets.Socket object.

Syntax

public IAsyncResult BeginSendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state)

Parameters

fileName
A string that contains the path and name of the file to be sent. This parameter can be null.
preBuffer
A byte array that contains data to be sent before the file is sent. This parameter can be null.
postBuffer
A byte array that contains data to be sent after the file is sent. This parameter can be null.
flags
A bitwise combination of System.Net.Sockets.TransmitFileOptions values.
callback
An AsyncCallback delegate to be invoked when this operation completes. This parameter can be null.
state
A user-defined object that contains state information for this request. This parameter can be null.

Returns

An IAsyncResult object that represents the asynchronous operation.

Remarks

This overload requires the name of the file you want to send and a bitwise combination of System.Net.Sockets.TransmitFileOptions values. The preBuffer parameter contains any data you want to precede the file. postBuffer contains data you want to follow the file. If fileName is in the local 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 ("..\\myfile.txt") and UNC share names ("\\\\shared directory\\myfile.txt") are supported. If the file is not found, the exception System.IO.FileNotFoundException is thrown.

The flags parameter provides the Window Sockets service provider with additional information about the file transfer. For more information about how to use this parameter, see System.Net.Sockets.TransmitFileOptions.

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

The Socket.BeginSendFile(string, AsyncCallback, object) method starts an asynchronous send operation to the remote host established in the Socket.Connect(System.Net.EndPoint), Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), Socket.Accept, or Socket.BeginAccept(AsyncCallback, object) methods. Socket.BeginSendFile(string, AsyncCallback, object) throws an exception if you do not first call Socket.Accept, Socket.BeginAccept(AsyncCallback, object), Socket.Connect(System.Net.EndPoint), or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object). Calling the Socket.BeginSendFile(string, AsyncCallback, object) method gives you the ability to send a file within a separate execution thread.

To complete the operation, you can create a callback method that is invoked by the AsyncCallback delegate parameter. To do this, at the very minimum, the state parameter must contain the System.Net.Sockets.Socket object being used for communication. If your callback needs more information, you can create a class or structure to hold the System.Net.Sockets.Socket and the other required information. Pass an instance of this custom object to the Socket.BeginSendFile(string, AsyncCallback, object) method through the state parameter.

Your callback method must invoke the Socket.EndSendFile(IAsyncResult) method. When your application calls Socket.BeginSendFile(string, AsyncCallback, object), the system uses a separate thread to execute the specified callback method, and blocks on Socket.EndSendFile(IAsyncResult) until the System.Net.Sockets.Socket sends the entire file or throws an exception. For additional information on writing callback methods see [<topic://cpconcallbacksample>].

Although intended for connection-oriented protocols, Socket.BeginSendFile(string, AsyncCallback, object) also works for connectionless protocols, provided that you first call the Socket.Connect(System.Net.EndPoint) or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) 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 Socket.BeginSendFile(string, AsyncCallback, object) throws a System.Net.Sockets.SocketException exception.

Note:

If you receive a System.Net.Sockets.SocketException exception, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous System.Net.Sockets.Socket methods. After the first use of a particular context (a specific asynchronous System.Net.Sockets.Socket method, a specific System.Net.Sockets.Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0