Socket.SendTo 方法

定义

将数据发送到特定终结点。Sends data to a specific endpoint.

重载

SendTo(Byte[], EndPoint)

将数据发送到指定的终结点。Sends data to the specified endpoint.

SendTo(Byte[], SocketFlags, EndPoint)

使用指定的 SocketFlags,将数据发送到特定的终结点。Sends data to a specific endpoint using the specified SocketFlags.

SendTo(Byte[], Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags,将指定字节数的数据发送到指定的终结点。Sends the specified number of bytes of data to the specified endpoint using the specified SocketFlags.

SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags,将指定字节数的数据发送到指定终结点(从缓冲区中的指定位置开始)。Sends the specified number of bytes of data to the specified endpoint, starting at the specified location in the buffer, and using the specified SocketFlags.

SendTo(Byte[], EndPoint)

将数据发送到指定的终结点。Sends data to the specified endpoint.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * System.Net.EndPoint -> int
Public Function SendTo (buffer As Byte(), remoteEP As EndPoint) As Integer

参数

buffer
Byte[]

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

remoteEP
EndPoint

EndPoint,它表示数据的目标位置。The EndPoint that represents the destination for the data.

返回

已发送的字节数。The number of bytes sent.

异常

buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

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

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例将无连接数据报发送到指定的远程主机。The following code example sends a connectionless datagram to the specified remote host.

static void SendTo1()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, endPoint );
   s->Close();
}
public static void SendTo1()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks. 
    s.SendTo(msg, endPoint);
    s.Close();
}
Public Shared Sub SendTo1() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, endPoint)
    s.Close()

End Sub

注解

在此重载中,缓冲区偏移量默认为0,发送的字节数默认为 buffer 参数的大小,SocketFlags 值默认为0。In this overload, the buffer offset defaults to 0, the number of bytes to send defaults to the size of the buffer parameter, and the SocketFlags value defaults to 0.

如果使用的是无连接协议,则在调用 SendTo之前,无需使用 Connect 方法建立默认远程主机。If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. 如果要调用 Send 方法,只需执行此操作。You only need to do this if you intend to call the Send method. 如果在调用 SendTo之前调用 Connect 方法,remoteEP 参数将重写该发送操作的指定默认远程主机。If you do call the Connect 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, because the underlying service provider will assign the most appropriate local network address and port number. 如果需要确定分配的本地网络地址和端口号,可以在 SendTo 方法成功完成后使用 LocalEndPoint 属性。If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

尽管适用于无连接协议,但 SendTo 也适用于面向连接的协议。Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. 如果使用面向连接的协议,则必须先通过调用 Connect 方法来建立远程主机连接,或使用 Accept 方法接受传入的连接请求。If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. 如果未建立或接受远程主机连接,SendTo 将引发 SocketExceptionIf you do not establish or accept a remote host connection, SendTo will throw a SocketException. 还可以在调用 SendTo 方法之前,为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. 在这两种情况下,SendTo 将忽略 remoteEP 参数,只将数据发送到已连接或默认的远程主机。In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

在发送缓冲区中的所有字节之前,阻止套接字将会阻止。Blocking sockets will block until the all of the bytes in the buffer are sent. 由于非阻止 Socket 会立即完成,因此它可能不会发送 buffer中的所有字节。Since a nonblocking Socket completes immediately, it might not send all of the bytes in the buffer. 应用程序负责跟踪发送的字节数,并重试该操作,直到应用程序发送 buffer中的所有字节。It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends all of the bytes in the buffer. 还不能保证发送的数据会立即显示在网络上。There is also no guarantee that the data you send will appear on the network immediately. 为了提高网络效率,基础系统可能会延迟传输,直到收集大量传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. 成功完成 SendTo 方法意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

如果在阻止模式下使用无连接协议,则在发送数据报之前,将阻止 SendToIf you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. 如果要将数据发送到广播地址,则必须先调用 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 number of bytes sent does not exceed the maximum packet size of the underlying service provider. 如果是这样,则将不发送数据报并且 SendTo 将引发 SocketExceptionIf it does, the datagram will not be sent and SendTo will throw a SocketException.

备注

如果收到 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.

另请参阅

SendTo(Byte[], SocketFlags, EndPoint)

使用指定的 SocketFlags,将数据发送到特定的终结点。Sends data to a specific endpoint using the specified SocketFlags.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int

参数

buffer
Byte[]

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

socketFlags
SocketFlags

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

remoteEP
EndPoint

EndPoint,它表示数据的目标位置。The EndPoint that represents the destination location for the data.

返回

已发送的字节数。The number of bytes sent.

异常

buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

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

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例将无连接数据报发送到指定的远程主机。The following code example sends a connectionless datagram to the specified remote host. SocketFlags 传递到 SendTo 方法。SocketFlags are passed to the SendTo method.

static void SendTo2()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, SocketFlags::None, endPoint );
   s->Close();
}
public static void SendTo2()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks. 
    s.SendTo(msg, SocketFlags.None, endPoint);
    s.Close();
}
Public Shared Sub SendTo2() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, SocketFlags.None, endPoint)
    s.Close()

End Sub

注解

在此重载中,缓冲区偏移量默认为0,发送的字节数默认为 buffer的大小。In this overload, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer. 如果指定 DontRoute 标志作为 socketflags 参数,则将不路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

如果使用的是无连接协议,则在调用 SendTo之前,无需使用 Connect 方法建立默认远程主机。If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. 如果要调用 Send 方法,只需执行此操作。You only need to do this if you intend to call the Send method. 如果在调用 SendTo之前调用 Connect 方法,remoteEP 参数将重写该发送操作的指定默认远程主机。If you do call the Connect 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, because the underlying service provider will assign the most appropriate local network address and port number. 如果需要确定分配的本地网络地址和端口号,可以在 SendTo 方法成功完成后使用 LocalEndPoint 属性。If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

尽管适用于无连接协议,但 SendTo 也适用于面向连接的协议。Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. 如果使用面向连接的协议,则必须先通过调用 Connect 方法来建立远程主机连接,或使用 Accept 方法接受传入的连接请求。If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. 如果未建立或接受远程主机连接,SendTo 将引发 SocketExceptionIf you do not establish or accept a remote host connection, SendTo will throw a SocketException. 还可以在调用 SendTo 方法之前,为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. 在这两种情况下,SendTo 将忽略 remoteEP 参数,只将数据发送到已连接或默认的远程主机。In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

阻止套接字将会阻塞,直到发送了 buffer 中的所有字节。Blocking sockets will block until the requested all of the bytes in the buffer are sent. 由于非阻止 Socket 会立即完成,因此它可能不会发送 buffer中的所有字节。Since a nonblocking Socket completes immediately, it might not send all of the bytes in the buffer. 应用程序负责跟踪发送的字节数,并重试该操作,直到应用程序发送 buffer中的所有字节。It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends all of the bytes in the buffer. 还不能保证发送的数据会立即显示在网络上。There is also no guarantee that the data you send will appear on the network immediately. 为了提高网络效率,基础系统可能会延迟传输,直到收集了大量的传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of out-going data is collected. 成功完成 SendTo 方法意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

如果在阻止模式下使用无连接协议,则在发送数据报之前,将阻止 SendToIf you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. 如果要将数据发送到广播地址,则必须先调用 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 number of bytes sent does not exceed the maximum packet size of the underlying service provider. 如果是这样,则将不发送数据报并且 SendTo 将引发 SocketExceptionIf it does, the datagram will not be sent and SendTo will throw a SocketException.

备注

如果收到 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.

另请参阅

SendTo(Byte[], Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags,将指定字节数的数据发送到指定的终结点。Sends the specified number of bytes of data to the specified endpoint using the specified SocketFlags.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, int size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int

参数

buffer
Byte[]

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

size
Int32

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

socketFlags
SocketFlags

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

remoteEP
EndPoint

EndPoint,它表示数据的目标位置。The EndPoint that represents the destination location for the data.

返回

已发送的字节数。The number of bytes sent.

异常

buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

指定的 size 超出 buffer 的大小。The specified size exceeds the size of buffer.

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

Socket 已关闭。The Socket has been closed.

示例

下面的代码示例将无连接数据报发送到指定的远程主机。The following code example sends a connectionless datagram to the specified remote host. 将大小和 SocketFlags 传递到 SendTo 方法。The size and SocketFlags are passed to the SendTo method.

static void SendTo3()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, msg->Length, SocketFlags::None, endPoint );
   s->Close();
}
public static void SendTo3()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks. 
    s.SendTo(msg, msg.Length, SocketFlags.None, endPoint);
    s.Close();
}
Public Shared Sub SendTo3() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, msg.Length, SocketFlags.None, endPoint)
    s.Close()

End Sub

注解

在此重载中,缓冲区偏移量默认为0。In this overload, the buffer offset defaults to 0. 如果指定 DontRoute 标志作为 socketflags 参数,则将不路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

如果使用的是无连接协议,则在调用 SendTo之前,无需使用 Connect 方法建立默认远程主机。If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. 如果要调用 Send 方法,只需执行此操作。You only need to do this if you intend to call the Send method. 如果在调用 SendTo之前调用 Connect 方法,remoteEP 参数将重写该发送操作的指定默认远程主机。If you do call the Connect 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, because the underlying service provider will assign the most appropriate local network address and port number. 如果需要确定分配的本地网络地址和端口号,可以在 SendTo 方法成功完成后使用 LocalEndPoint 属性。If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

尽管适用于无连接协议,但 SendTo 也适用于面向连接的协议。Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. 如果使用面向连接的协议,则必须先通过调用 Connect 方法来建立远程主机连接,或使用 Accept 方法接受传入的连接请求。If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. 如果未建立或接受远程主机连接,SendTo 将引发 SocketExceptionIf you do not establish or accept a remote host connection, SendTo will throw a SocketException. 还可以在调用 SendTo 方法之前,为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. 在这两种情况下,SendTo 将忽略 remoteEP 参数,只将数据发送到已连接或默认的远程主机。In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

阻止套接字将会阻止,直到发送请求的字节数。Blocking sockets will block until the requested number of bytes are sent. 由于非阻止 Socket 会立即完成,因此它可能不会在单个操作中发送所有请求的字节。Since a nonblocking Socket completes immediately, it might not send all of the bytes requested in a single operation. 应用程序负责跟踪发送的字节数,并重试操作,直到应用程序发送请求的字节数。It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. 还不能保证发送的数据会立即显示在网络上。There is also no guarantee that the data you send will appear on the network immediately. 为了提高网络效率,基础系统可能会延迟传输,直到收集了大量的传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of out-going data is collected. 成功完成 SendTo 方法意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

如果在阻止模式下使用无连接协议,则在发送数据报之前,将阻止 SendToIf you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. 如果要将数据发送到广播地址,则必须先调用 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 number of bytes sent does not exceed the maximum packet size of the underlying service provider. 如果是这样,则将不发送数据报并且 SendTo 将引发 SocketExceptionIf it does, the datagram will not be sent and SendTo will throw a SocketException.

备注

如果收到 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.

另请参阅

SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags,将指定字节数的数据发送到指定终结点(从缓冲区中的指定位置开始)。Sends the specified number of bytes of data to the specified endpoint, starting at the specified location in the buffer, and using the specified SocketFlags.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * int * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int

参数

buffer
Byte[]

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

offset
Int32

开始发送数据的缓冲区中的位置。The position in the data buffer at which to begin sending data.

size
Int32

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

socketFlags
SocketFlags

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

remoteEP
EndPoint

EndPoint,它表示数据的目标位置。The EndPoint that represents the destination location for the data.

返回

已发送的字节数。The number of bytes sent.

异常

buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

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.

socketFlags 不是有效的值组合。socketFlags is not a valid combination of values.

-or- 访问 Socket 时出现操作系统错误。An operating system error occurs while accessing the Socket.

Socket 已关闭。The Socket has been closed.

调用堆栈中的调用方没有所需的权限。A caller in the call stack does not have the required permissions.

示例

下面的代码示例将无连接数据报发送到指定的远程主机。The following code example sends a connectionless datagram to the specified remote host. 偏移量、大小和 SocketFlags 传递到 SendTo 方法。The offset, size, and SocketFlags are passed to the SendTo method.

static void SendTo4()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, 0, msg->Length, SocketFlags::None, endPoint );
   s->Close();
}
public static void SendTo4()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);
  
    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks. 
    s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint);
    s.Close();
}

Public Shared Sub SendTo4() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint)
    s.Close()

End Sub


注解

在此重载中,如果指定 DontRoute 标志作为 socketflags 参数,则将不会路由你要发送的数据。In this overload, if you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

如果使用的是无连接协议,则在调用 SendTo之前,无需使用 Connect 方法建立默认远程主机。If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. 如果要调用 Send 方法,只需执行此操作。You only need to do this if you intend to call the Send method. 如果在调用 SendTo之前调用 Connect 方法,remoteEP 参数将重写该发送操作的指定默认远程主机。If you do call the Connect 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, because the underlying service provider will assign the most appropriate local network address and port number. 如果需要确定分配的本地网络地址和端口号,可以在 SendTo 方法成功完成后使用 LocalEndPoint 属性。If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

尽管适用于无连接协议,但 SendTo 也适用于面向连接的协议。Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. 如果使用面向连接的协议,则必须先通过调用 Connect 方法来建立远程主机连接,或使用 Accept 方法接受传入的连接请求。If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. 如果未建立或接受远程主机连接,SendTo 将引发 SocketExceptionIf you do not establish or accept a remote host connection, SendTo will throw a SocketException. 还可以在调用 SendTo 方法之前,为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. 在这两种情况下,SendTo 将忽略 remoteEP 参数,只将数据发送到已连接或默认的远程主机。In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

阻止套接字将会阻止,直到发送请求的字节数。Blocking sockets will block until the requested number of bytes are sent. 由于非阻止性 Socket 会立即完成,因此它可能不会在单个操作中发送所有请求的字节。Since a non-blocking Socket completes immediately, it might not send all of the bytes requested in a single operation. 应用程序负责跟踪发送的字节数,并重试操作,直到应用程序发送请求的字节数。It is your applications responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. 还不能保证发送的数据会立即显示在网络上。There is also no guarantee that the data you send will appear on the network immediately. 为了提高网络效率,基础系统可能会延迟传输,直到收集了大量的传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of out-going data is collected. 成功完成 SendTo 方法意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

如果在阻止模式下使用无连接协议,则在发送数据报之前,将阻止 SendToIf you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. 如果要将数据发送到广播地址,则必须先调用 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 does not exceed the maximum packet size of the underlying service provider. 如果是这样,则将不发送数据报并且 SendTo 将引发 SocketExceptionIf it does, the datagram will not be sent and SendTo will throw a SocketException.

备注

如果收到 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.

另请参阅

适用于