Socket.BeginSendTo(Byte[], Int32, Int32, SocketFlags, EndPoint, AsyncCallback, Object) Socket.BeginSendTo(Byte[], Int32, Int32, SocketFlags, EndPoint, AsyncCallback, Object) Socket.BeginSendTo(Byte[], Int32, Int32, SocketFlags, EndPoint, AsyncCallback, Object) Socket.BeginSendTo(Byte[], Int32, Int32, SocketFlags, EndPoint, AsyncCallback, Object) Method


以异步方式将数据发送到特定远程主机。Sends data asynchronously to a specific remote host.

 IAsyncResult ^ BeginSendTo(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socket_flags, System::Net::EndPoint ^ remote_end, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSendTo (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socket_flags, System.Net.EndPoint remote_end, AsyncCallback callback, object state);
member this.BeginSendTo : byte[] * int * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint * AsyncCallback * obj -> IAsyncResult
Public Function BeginSendTo (buffer As Byte(), offset As Integer, size As Integer, socket_flags As SocketFlags, remote_end As EndPoint, callback As AsyncCallback, state As Object) As IAsyncResult



Byte 类型的数组,它包含要发送的数据。An array of type Byte that contains the data to send.

Int32 Int32 Int32 Int32

buffer 中的从其开始发送数据的、从零开始编排的位置。The zero-based position in buffer at which to begin sending data.

Int32 Int32 Int32 Int32

要发送的字节数。The number of bytes to send.

SocketFlags SocketFlags SocketFlags SocketFlags

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

EndPoint EndPoint EndPoint EndPoint

表示远程设备的 EndPointAn EndPoint that represents the remote device.

Object Object Object Object

一个对象,它包含此请求的状态信息。An object that contains state information for this request.


引用异步发送的 IAsyncResultAn IAsyncResult that references the asynchronous send.


buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

offset 小于 0。offset is less than 0.

- 或 --or- offset 大于 buffer 的长度。offset is greater than the length of buffer.

-or- size 小于 0。size is less than 0.

- 或 --or- size 大于 buffer 的长度减去 offset 参数的值。size is greater than the length of buffer minus the value of the offset parameter.

调用堆栈中的较高调用方无权执行所请求的操作。A caller higher in the call stack does not have permission for the requested operation.


下面的代码示例将数据异步发送到指定的远程主机。The following code example asynchronously sends data to the specified remote host.

IPHostEntry^ lipa = Dns::Resolve( "" );
IPEndPoint^ lep = gcnew IPEndPoint( lipa->AddressList[ 0 ], 11000 );

Socket^ s = gcnew Socket( lep->Address->AddressFamily,
   ProtocolType::Tcp );
   while ( true )

      array<Byte>^ buff = Encoding::ASCII->GetBytes( "This is a test" );

      Console::WriteLine( "Sending Message Now.." );
      s->BeginSendTo( buff, 0, buff->Length, SocketFlags::None, lep,
         gcnew AsyncCallback( &Async_Send_Receive::Connect_Callback ), s );

catch ( Exception^ e ) 
   Console::WriteLine( e );
IPHostEntry lipa = Dns.Resolve("");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);

      Socket s = new Socket(lep.Address.AddressFamily,

                byte[] buff = Encoding.ASCII.GetBytes("This is a test");
                Console.WriteLine("Sending Message Now..");
                s.BeginSendTo(buff, 0, buff.Length, 0, lep, new AsyncCallback(Async_Send_Receive.SendTo_Callback), s);

      catch (Exception e){
   Dim lipa As IPHostEntry = Dns.Resolve("")
   Dim lep As New IPEndPoint(lipa.AddressList(0), 11000)
   Dim s As New Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
      While True
         Dim buff As Byte() = Encoding.ASCII.GetBytes("This is a test")
         Console.WriteLine("Sending Message Now..")
         s.BeginSendTo(buff, 0, buff.Length, 0, lep, New AsyncCallback(AddressOf Async_Send_Receive.SendTo_Callback), s)
      End While
   Catch e As Exception
   End Try
End Sub 'SendTo


方法对remoteEP参数中指定的远程主机启动异步发送操作。 BeginSendToThe BeginSendTo method starts an asynchronous send operation to the remote host specified in the remoteEP parameter. 通过调用BeginSendTo方法, 可以在单独的执行线程中发送数据。Calling the BeginSendTo method gives you the ability to send data within a separate execution thread. 尽管适用于无连接协议BeginSendTo , 但适用于连接和面向连接的协议。Although intended for connectionless protocols, BeginSendTo works with both connectionless and connection-oriented protocols.

你可以创建一个实现AsyncCallback委托的回调方法, 并将其名称传递BeginSendTo给方法。You can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSendTo method. 为此, 你state的参数至少必须包含用于通信的已连接或默认值。 SocketTo do this, at the very minimum, your state parameter must contain the connected or default Socket being used for communication. 如果回调需要更多的Socket信息, 可以创建一个小类来保存和其他所需的信息。If your callback needs more information, you can create a small class to hold the Socket, and the other required information. 通过参数将此类的一个实例BeginSendTo传递给方法。 statePass an instance of this class to the BeginSendTo method through the state parameter.

回调方法应调用EndSendTo方法。Your callback method should invoke the EndSendTo method. 当应用程序调用BeginSendTo时, 系统将使用一个单独的线程来执行指定的回调方法, 并在EndSendTo Socket发送请求的字节数或引发异常之前阻止。When your application calls BeginSendTo, the system will use a separate thread to execute the specified callback method, and will block on EndSendTo until the Socket sends the number of bytes requested or throws an exception. 如果希望在调用BeginSendTo方法后原始线程会被阻止, 请WaitHandle.WaitOne使用方法。If you want the original thread to block after you call the BeginSendTo method, use the WaitHandle.WaitOne method. 如果希望原始线程继续执行, 请在回调方法中的 T:System.Threading.ManualResetEvent 上调用 Set 方法。Call the Set method on a T:System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing. 有关编写回调方法的其他信息, 请参阅将委托作为回调方法进行封送处理For additional information about writing callback methods see Marshaling a Delegate as a Callback Method.

如果使用面向连接的协议, 则必须先调用ConnectBeginConnectAcceptBeginAccept方法, 否则BeginSendTo将引发SocketExceptionIf you are using a connection-oriented protocol, you must first call the Connect, BeginConnect, Accept, or BeginAccept method, or BeginSendTo will throw a SocketException. BeginSendToremoteEP忽略参数, 并将数据发送到EndPoint ConnectBeginConnect Accept、或BeginAccept方法中建立的。BeginSendTo will ignore the remoteEP parameter and send data to the EndPoint established in the Connect, BeginConnect, Accept, or BeginAccept method.

如果使用的是无连接协议, 则在调用Connect SendTo之前, 无需使用或BeginConnect方法建立默认远程主机。If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect or BeginConnect method prior to calling SendTo. 如果要调用BeginSend方法, 只需执行此操作。You only need to do this if you intend to call the BeginSend method. 如果Connect在调用BeginConnect remoteEP之前调用了或方法, 则该参数将仅为该发送操作重写指定的默认远程主机。 SendToIf you do call the Connect or BeginConnect method prior to calling SendTo, the remoteEP parameter will override the specified default remote host for that send operation only. 您也不需要调用Bind方法。You are also not required to call the Bind method. 在这种情况下, 基础服务提供程序将分配最适当的本地网络地址和端口号。In this case, the underlying service provider will assign the most appropriate local network address and port number. 如果希望基础服务提供商选择可用端口, 请使用端口号零。Use a port number of zero if you want the underlying service provider to select a free port. 如果需要确定分配的本地网络地址和端口号, 可以在LocalEndPoint EndSendTo方法成功完成后使用属性。If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the EndSendTo method successfully completes.

如果要将数据发送到广播地址, 则必须先调用SetSocketOption方法, 并将套接字选项设置为。 SocketOptionName.BroadcastIf you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast. -还必须确保缓冲区的大小不超过基础服务提供程序的最大数据包大小。-You must also be sure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. 如果是这样, 则不会发送数据报并EndSendToSocketException引发。If it does, the datagram will not be sent and EndSendTo will throw a SocketException.

如果指定DontRoute标志socketflags作为参数, 则不会路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.


如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.


当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.


为异步Socket方法缓存执行上下文 (安全上下文、模拟用户和调用上下文)。The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. 第一次使用特定的上下文 (特定的异步Socket方法、特定Socket的实例和特定的回调) 后, 该上下文的后续使用将会提高性能。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.