Redigeeri

Jagamisviis:


Socket.BeginSendFile Method

Definition

Sends a file asynchronously to a connected Socket object.

Overloads

BeginSendFile(String, AsyncCallback, Object)

Sends the file fileName to a connected Socket object using the UseDefaultWorkerThread flag.

BeginSendFile(String, Byte[], Byte[], TransmitFileOptions, AsyncCallback, Object)

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

BeginSendFile(String, AsyncCallback, Object)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Sends the file fileName to a connected Socket object using the UseDefaultWorkerThread flag.

public:
 IAsyncResult ^ BeginSendFile(System::String ^ fileName, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSendFile (string fileName, AsyncCallback? callback, object? state);
public IAsyncResult BeginSendFile (string? fileName, AsyncCallback? callback, object? state);
public IAsyncResult BeginSendFile (string fileName, AsyncCallback callback, object state);
member this.BeginSendFile : string * AsyncCallback * obj -> IAsyncResult
Public Function BeginSendFile (fileName As String, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

fileName
String

A string that contains the path and name of the file to send. This parameter can be null.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult object that represents the asynchronous send.

Exceptions

The Socket object has been closed.

The socket is not connected to a remote host.

The file fileName was not found.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket. See remarks section below.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

This overload sends the file fileName over the socket. 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 FileNotFoundException is thrown.

You can pass a callback that implements AsyncCallback to BeginSendFile in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSendFile. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSendFile method.

The BeginSendFile operation must be completed by calling the EndSendFile method. Typically, the method is invoked by the AsyncCallback delegate. EndSendFile will block the calling thread until the operation is completed.

Although intended for connection-oriented protocols, BeginSendFile also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. With connectionless protocols, you must 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 BeginSendFile throws a SocketException exception.

Note

If you receive a SocketException exception, use the SocketException.ErrorCode property to obtain the specific error code.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

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

Applies to

BeginSendFile(String, Byte[], Byte[], TransmitFileOptions, AsyncCallback, Object)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

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

public:
 IAsyncResult ^ BeginSendFile(System::String ^ fileName, cli::array <System::Byte> ^ preBuffer, cli::array <System::Byte> ^ postBuffer, System::Net::Sockets::TransmitFileOptions flags, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSendFile (string? fileName, byte[]? preBuffer, byte[]? postBuffer, System.Net.Sockets.TransmitFileOptions flags, AsyncCallback? callback, object? state);
public IAsyncResult BeginSendFile (string fileName, byte[] preBuffer, byte[] postBuffer, System.Net.Sockets.TransmitFileOptions flags, AsyncCallback callback, object state);
member this.BeginSendFile : string * byte[] * byte[] * System.Net.Sockets.TransmitFileOptions * AsyncCallback * obj -> IAsyncResult
Public Function BeginSendFile (fileName As String, preBuffer As Byte(), postBuffer As Byte(), flags As TransmitFileOptions, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

fileName
String

A string that contains the path and name of the file to be sent. This parameter can be null.

preBuffer
Byte[]

The data to be sent before the file is sent. This parameter can be null.

postBuffer
Byte[]

The data to be sent after the file is sent. This parameter can be null.

flags
TransmitFileOptions

A bitwise combination of the enumeration values.

callback
AsyncCallback

An AsyncCallback delegate to be invoked when this operation completes. This parameter can be null.

state
Object

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.

Exceptions

The Socket object has been closed.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket. See remarks section below.

The operating system is not Windows NT or later.

-or-

The socket is not connected to a remote host.

The file fileName was not found.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

This overload requires the name of the file you want to send and a bitwise combination of 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 FileNotFoundException is thrown.

The flags parameter provides additional information about the file transfer. For more information about how to use this parameter, see TransmitFileOptions.

You can pass a callback that implements AsyncCallback to BeginSendFile in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSendFile. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSendFile method.

The BeginSendFile operation must be completed by calling the EndSendFile method. Typically, the method is invoked by the provided AsyncCallback delegate. EndSendFile will block the calling thread until the operation is completed.

Although intended for connection-oriented protocols, BeginSendFile also works for connectionless protocols, provided that you first call the Connect or BeginConnect method to establish a default remote host. With connectionless protocols, you must 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 BeginSendFile throws a SocketException exception.

Note

If you receive a SocketException exception, use the SocketException.ErrorCode property to obtain the specific error code.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

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

Applies to