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 SendTo之前,不需要使用方法建立默认远程主机。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. 如果需要确定分配的本地网络地址和端口号,可以在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. 如果未建立或接受远程主机连接, SendToSocketException引发。If 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.

如果在阻止模式下使用无连接协议,则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. 如果是这样,则不会发送数据报并SendToSocketException引发。If 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 SendTo之前,不需要使用方法建立默认远程主机。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. 如果需要确定分配的本地网络地址和端口号,可以在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. 如果未建立或接受远程主机连接, SendToSocketException引发。If 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.

如果在阻止模式下使用无连接协议,则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. 如果是这样,则不会发送数据报并SendToSocketException引发。If 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 SendTo之前,不需要使用方法建立默认远程主机。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. 如果需要确定分配的本地网络地址和端口号,可以在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. 如果未建立或接受远程主机连接, SendToSocketException引发。If 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. 如果是这样,则不会发送数据报并SendToSocketException引发。If 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 SendTo之前,不需要使用方法建立默认远程主机。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. 如果需要确定分配的本地网络地址和端口号,可以在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. 如果未建立或接受远程主机连接, SendToSocketException引发。If 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. 如果是这样,则不会发送数据报并SendToSocketException引发。If 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.

另请参阅

适用于