Socket.SendTo Socket.SendTo Socket.SendTo Socket.SendTo Method

定义

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

重载

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

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

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

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

SendTo(Byte[], Int32, SocketFlags, EndPoint) SendTo(Byte[], Int32, SocketFlags, EndPoint) 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) SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint) 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) SendTo(Byte[], EndPoint) SendTo(Byte[], EndPoint) 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 EndPoint 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.

示例

下面的代码示例将无连接的数据报发送到指定的远程主机。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 'SendTo1

注解

此重载中的缓冲区偏移量的默认值为 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.

如果使用的是无连接协议,不需要建立默认远程主机使用Connect方法之前调用SendToIf 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. 如果调用Connect方法之前调用SendTo,则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. 如果你需要标识分配的本地网络地址和端口号,则可以使用LocalEndPoint属性后的SendTo方法成功完成。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完成立即,它可能不会发送的字节中的所有bufferSince a nonblocking Socket completes immediately, it might not send all of the bytes in the buffer. 它是应用程序负责跟踪发送的字节数并重试操作,直到应用程序发送的字节中的所有bufferIt 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.

如果在阻止模式下,使用无连接协议SendTo会阻塞,直到发送数据报。If 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) SendTo(Byte[], SocketFlags, EndPoint) 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 SocketFlags SocketFlags

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

remoteEP
EndPoint EndPoint EndPoint 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.

示例

下面的代码示例将无连接的数据报发送到指定的远程主机。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 'SendTo2

注解

此重载中的缓冲区偏移量的默认值为 0,并要将默认值发送到的大小的字节数bufferIn 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.

如果使用的是无连接协议,不需要建立默认远程主机使用Connect方法之前调用SendToIf 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. 如果调用Connect方法之前调用SendTo,则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. 如果你需要标识分配的本地网络地址和端口号,则可以使用LocalEndPoint属性后的SendTo方法成功完成。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完成立即,它可能不会发送的字节中的所有bufferSince a nonblocking Socket completes immediately, it might not send all of the bytes in the buffer. 它是应用程序负责跟踪发送的字节数并重试操作,直到应用程序发送的字节中的所有bufferIt 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.

如果在阻止模式下,使用无连接协议SendTo会阻塞,直到发送数据报。If 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) SendTo(Byte[], Int32, SocketFlags, EndPoint) 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 Int32 Int32 Int32

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

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

remoteEP
EndPoint EndPoint EndPoint 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.

示例

下面的代码示例将无连接的数据报发送到指定的远程主机。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 'SendTo3

注解

此重载中的缓冲区偏移量的默认值为 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.

如果使用的是无连接协议,不需要建立默认远程主机使用Connect方法之前调用SendToIf 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. 如果调用Connect方法之前调用SendTo,则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. 如果你需要标识分配的本地网络地址和端口号,则可以使用LocalEndPoint属性后的SendTo方法成功完成。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.

如果在阻止模式下,使用无连接协议SendTo会阻塞,直到发送数据报。If 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) SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint) 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 Int32 Int32 Int32

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

size
Int32 Int32 Int32 Int32

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

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

remoteEP
EndPoint EndPoint EndPoint 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.

调用堆栈中的调用方没有所需的权限。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 'SendTo4


注解

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

如果使用的是无连接协议,不需要建立默认远程主机使用Connect方法之前调用SendToIf 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. 如果调用Connect方法之前调用SendTo,则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. 如果你需要标识分配的本地网络地址和端口号,则可以使用LocalEndPoint属性后的SendTo方法成功完成。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.

如果在阻止模式下,使用无连接协议SendTo会阻塞,直到发送数据报。If 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.

另请参阅

适用于