Socket.Send Socket.Send Socket.Send Socket.Send Method

定义

将数据发送到连接的 SocketSends data to a connected Socket.

重载

Send(Byte[]) Send(Byte[]) Send(Byte[]) Send(Byte[])

将数据发送到连接的 SocketSends data to a connected Socket.

Send(IList<ArraySegment<Byte>>) Send(IList<ArraySegment<Byte>>) Send(IList<ArraySegment<Byte>>) Send(IList<ArraySegment<Byte>>)

将列表中的一组缓冲区发送到连接的 SocketSends the set of buffers in the list to a connected Socket.

Send(ReadOnlySpan<Byte>) Send(ReadOnlySpan<Byte>) Send(ReadOnlySpan<Byte>) Send(ReadOnlySpan<Byte>)
Send(Byte[], SocketFlags) Send(Byte[], SocketFlags) Send(Byte[], SocketFlags)

使用指定的 Socket 将数据发送到连接的 SocketFlagsSends data to a connected Socket using the specified SocketFlags.

Send(IList<ArraySegment<Byte>>, SocketFlags) Send(IList<ArraySegment<Byte>>, SocketFlags) Send(IList<ArraySegment<Byte>>, SocketFlags)

使用指定的 Socket,将列表中的一组缓冲区发送到连接的 SocketFlagsSends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

Send(ReadOnlySpan<Byte>, SocketFlags) Send(ReadOnlySpan<Byte>, SocketFlags) Send(ReadOnlySpan<Byte>, SocketFlags)
Send(Byte[], Int32, SocketFlags) Send(Byte[], Int32, SocketFlags) Send(Byte[], Int32, SocketFlags)

使用指定的 Socket,将指定字节数的数据发送到已连接的 SocketFlagsSends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.

Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError) Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError) Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError)

使用指定的 Socket,将列表中的一组缓冲区发送到连接的 SocketFlagsSends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

Send(ReadOnlySpan<Byte>, SocketFlags, SocketError) Send(ReadOnlySpan<Byte>, SocketFlags, SocketError) Send(ReadOnlySpan<Byte>, SocketFlags, SocketError)
Send(Byte[], Int32, Int32, SocketFlags) Send(Byte[], Int32, Int32, SocketFlags) Send(Byte[], Int32, Int32, SocketFlags)

从指定偏移量开始,使用指定 SocketFlags,将指定的数据字节数发送到已连接的 SocketSends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

Send(Byte[], Int32, Int32, SocketFlags, SocketError) Send(Byte[], Int32, Int32, SocketFlags, SocketError) Send(Byte[], Int32, Int32, SocketFlags, SocketError)

从指定的偏移量开始使用指定的 Socket 将指定字节数的数据发送到连接的 SocketFlagsSends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags

Send(Byte[]) Send(Byte[]) Send(Byte[]) Send(Byte[])

将数据发送到连接的 SocketSends data to a connected Socket.

public:
 int Send(cli::array <System::Byte> ^ buffer);
public int Send (byte[] buffer);
member this.Send : byte[] -> int
Public Function Send (buffer As Byte()) As Integer

参数

buffer
Byte[]

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

返回

发送到 Socket 的字节数。The number of bytes sent to the Socket.

异常

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

示例

下面的代码示例演示如何在连接Socket的上发送数据。The following code example demonstrates sending data on a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer.
int SendReceiveTest1( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      // Blocks until send returns.
      int byteCount = server->Send( msg );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes );
      if ( byteCount > 0 )
      {
         Console::WriteLine( Encoding::UTF8->GetString( bytes ) );
      }
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine( "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
      return ( e->ErrorCode );
   }
   return 0;
}
// Displays sending with a connected socket
// using the overload that takes a buffer.
public static int SendReceiveTest1(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int i = server.Send(msg);
        Console.WriteLine("Sent {0} bytes.", i);
        
        // Get reply from the server.
        i = server.Receive(bytes);
        Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer.
Public Shared Function SendReceiveTest1(ByVal server As Socket) As Integer 
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
    Try
        ' Blocks until send returns.
        Dim i As Integer = server.Send(msg)
        Console.WriteLine("Sent {0} bytes.", i)
        
        ' Get reply from the server.
        i = server.Receive(bytes)
        Console.WriteLine(Encoding.UTF8.GetString(bytes))
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest1

注解

Send将数据同步发送到ConnectAccept方法中指定的远程主机, 并返回成功发送的字节数。Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send可用于面向连接的协议和无连接的协议。Send can be used for both connection-oriented and connectionless protocols.

此重载需要包含要发送的数据的缓冲区。This overload requires a buffer that contains the data you want to send. 默认SocketFlags值为 0, 缓冲区偏移量默认为 0, 要发送的字节数默认为缓冲区的大小。The SocketFlags value defaults to 0, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer.

如果使用的是无连接协议, 则必须在Connect调用此方法之前调用, Send否则将引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用的是无连接协议并计划将数据发送到多个不同的主机, 则应SendTo使用方法。If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. 如果不使用SendTo方法, 则必须在每次调用Send前调用ConnectIf you do not use the SendTo method, you will have to call Connect before each call to Send. 即使已使用SendTo建立默认远程Connect主机, 也可以使用。You can use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议, Send则会一直阻止, 直到发送了缓冲区中的所有字节, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式Send下, 即使发送的字节数小于缓冲区中的字节数, 也可能成功完成。In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the 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 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. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

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

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

Send(IList<ArraySegment<Byte>>) Send(IList<ArraySegment<Byte>>) Send(IList<ArraySegment<Byte>>) Send(IList<ArraySegment<Byte>>)

将列表中的一组缓冲区发送到连接的 SocketSends the set of buffers in the list to a connected Socket.

public:
 int Send(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers);
public int Send (System.Collections.Generic.IList<ArraySegment<byte>> buffers);
member this.Send : System.Collections.Generic.IList<ArraySegment<byte>> -> int
Public Function Send (buffers As IList(Of ArraySegment(Of Byte))) As Integer

参数

buffers
IList<ArraySegment<Byte>>

ArraySegment<T> 类型的 Byte 的列表,它包含要发送的数据。A list of ArraySegment<T>s of type Byte that contains the data to be sent.

返回

发送到 Socket 的字节数。The number of bytes sent to the Socket.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参见下面的备注部分。See remarks section below.

注解

Send可用于面向连接的协议和无连接的协议。Send can be used for both connection-oriented and connectionless protocols.

此重载至少需要一个包含要发送的数据的缓冲区。This overload requires at least one buffer that contains the data you want to send.

如果使用的是无连接协议, 则必须在Connect调用此方法之前调用, Send否则将引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用的是无连接协议并计划将数据发送到多个不同的主机, 则应SendTo使用方法。If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. 如果不使用SendTo方法, 则必须在每次调用Send前调用ConnectIf you do not use the SendTo method, you will have to call Connect before each call to Send. 即使已使用SendTo建立默认远程Connect主机, 也可以使用。You can use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议, Send则会一直阻止, 直到发送了缓冲区中的所有字节, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式Send下, 即使发送的字节数小于缓冲区中的字节数, 也可能成功完成。In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the 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 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. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

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

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

Send(ReadOnlySpan<Byte>) Send(ReadOnlySpan<Byte>) Send(ReadOnlySpan<Byte>) Send(ReadOnlySpan<Byte>)

public:
 int Send(ReadOnlySpan<System::Byte> buffer);
public int Send (ReadOnlySpan<byte> buffer);
member this.Send : ReadOnlySpan<byte> -> int
Public Function Send (buffer As ReadOnlySpan(Of Byte)) As Integer

参数

buffer
ReadOnlySpan<Byte>

返回

Send(Byte[], SocketFlags) Send(Byte[], SocketFlags) Send(Byte[], SocketFlags)

使用指定的 Socket 将数据发送到连接的 SocketFlagsSends data to a connected Socket using the specified SocketFlags.

public:
 int Send(cli::array <System::Byte> ^ buffer, System::Net::Sockets::SocketFlags socketFlags);
public int Send (byte[] buffer, System.Net.Sockets.SocketFlags socketFlags);
member this.Send : byte[] * System.Net.Sockets.SocketFlags -> 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.

返回

发送到 Socket 的字节数。The number of bytes sent to the Socket.

异常

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

示例

下面的代码示例演示如何在连接Socket的上发送数据。The following code example demonstrates sending data on a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
int SendReceiveTest2( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      // Blocks until send returns.
      int byteCount = server->Send( msg, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes, SocketFlags::None );
      if ( byteCount > 0 )
      {
         Console::WriteLine( Encoding::UTF8->GetString( bytes ) );
      }
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine( "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
      return (e->ErrorCode);
   }
   return 0;
}
// Displays sending with a connected socket
// using the overload that takes a buffer and socket flags.
public static int SendReceiveTest2(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);
        
        // Get reply from the server.
        byteCount = server.Receive(bytes, SocketFlags.None);
        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer and socket flags.
Public Shared Function SendReceiveTest2(ByVal server As Socket) As Integer 
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
    Try
        ' Blocks until send returns.
        Dim byteCount As Integer = server.Send(msg, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", byteCount)
        
        ' Get reply from the server.
        byteCount = server.Receive(bytes, SocketFlags.None)
        If byteCount > 0 Then
            Console.WriteLine(Encoding.UTF8.GetString(bytes))
        End If
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest2

注解

Send将数据同步发送到ConnectAccept方法中建立的远程主机, 并返回成功发送的字节数。Send synchronously sends data to the remote host established in the Connect or Accept method and returns the number of bytes successfully sent. Send方法可用于面向连接的协议和无连接的协议。The Send method can be used for both connection-oriented and connectionless protocols.

此重载需要包含要发送的数据的缓冲区和的SocketFlags按位组合。This overload requires a buffer that contains the data you want to send and a bitwise combination of SocketFlags. 缓冲区偏移量默认为 0, 要发送的字节数默认为缓冲区的大小。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 value, the data you are sending will not be routed.

如果使用的是无连接协议, 则必须在Connect调用此方法之前调用, Send否则将引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用的是无连接协议并计划将数据发送到多个不同的主机, 则应SendTo使用方法。If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. 如果不使用SendTo方法, 则必须在每次调用之前Connect调用Send方法。If you do not use the SendTo method, you will have to call the Connect method before each call to Send. 即使已使用SendTo建立默认远程Connect主机, 也可以使用。You can use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议, Send则会一直阻止, 直到发送了缓冲区中的所有字节, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式Send下, 即使发送的字节数小于缓冲区中的字节数, 也可能成功完成。In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes in the 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 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 outgoing data is collected. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

必须确保缓冲区的大小不超过基础服务提供程序的最大数据包大小。You must ensure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. 如果是这样, 则不会发送数据报并SendSocketException引发。If it does, the datagram will not be sent and Send 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.

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

Send(IList<ArraySegment<Byte>>, SocketFlags) Send(IList<ArraySegment<Byte>>, SocketFlags) Send(IList<ArraySegment<Byte>>, SocketFlags)

使用指定的 Socket,将列表中的一组缓冲区发送到连接的 SocketFlagsSends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

public:
 int Send(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags);
public int Send (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags);
member this.Send : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags -> int

参数

buffers
IList<ArraySegment<Byte>>

ArraySegment<T> 类型的 Byte 的列表,它包含要发送的数据。A list of ArraySegment<T>s of type Byte that contains the data to be sent.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

返回

发送到 Socket 的字节数。The number of bytes sent to the Socket.

异常

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

注解

此重载至少需要一个包含要发送的数据的缓冲区。This overload requires at least one buffer that contains the data you want to send. SocketFlags值默认为0。The SocketFlags value 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调用此方法之前调用, Send否则将引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用的是无连接协议并计划将数据发送到多个不同的主机, 则应SendTo使用方法。If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. 如果不使用SendTo方法, 则必须在每次调用Send前调用ConnectIf you do not use the SendTo method, you will have to call Connect before each call to Send. 即使已使用SendTo建立默认远程Connect主机, 也可以使用。You can use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议, Send则会一直阻止, 直到发送了缓冲区中的所有字节, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式下, Send即使发送的字节数小于缓冲区中的字节数, 也可能成功完成。In non-blocking mode, Send may complete successfully even if it sends less than the number of bytes in the 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 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. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

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

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

Send(ReadOnlySpan<Byte>, SocketFlags) Send(ReadOnlySpan<Byte>, SocketFlags) Send(ReadOnlySpan<Byte>, SocketFlags)

public:
 int Send(ReadOnlySpan<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags);
public int Send (ReadOnlySpan<byte> buffer, System.Net.Sockets.SocketFlags socketFlags);
member this.Send : ReadOnlySpan<byte> * System.Net.Sockets.SocketFlags -> int

参数

buffer
ReadOnlySpan<Byte>

返回

Send(Byte[], Int32, SocketFlags) Send(Byte[], Int32, SocketFlags) Send(Byte[], Int32, SocketFlags)

使用指定的 Socket,将指定字节数的数据发送到已连接的 SocketFlagsSends the specified number of bytes of data to a connected Socket, using the specified SocketFlags.

public:
 int Send(cli::array <System::Byte> ^ buffer, int size, System::Net::Sockets::SocketFlags socketFlags);
public int Send (byte[] buffer, int size, System.Net.Sockets.SocketFlags socketFlags);
member this.Send : byte[] * int * System.Net.Sockets.SocketFlags -> 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.

返回

发送到 Socket 的字节数。The number of bytes sent to the Socket.

异常

size 小于 0 或超过缓冲区的大小。size is less than 0 or exceeds the size of the buffer.

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

-or- 访问套接字时发生操作系统错误。An operating system error occurs while accessing the socket.

示例

下面的代码示例发送缓冲区中找到的数据, 并为None SocketFlags指定。The following code example sends the data found in buffer, and specifies None for SocketFlags.

// Displays sending with a connected socket
// using the overload that takes a buffer, message size, and socket flags.
int SendReceiveTest3( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      // Blocks until send returns.
      int i = server->Send( msg, msg->Length, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", i.ToString() );
      
      // Get reply from the server.
      int byteCount = server->Receive( bytes, server->Available,
         SocketFlags::None );
      if ( byteCount > 0 )
      {
         Console::WriteLine( Encoding::UTF8->GetString( bytes ) );
      }
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine( "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
      return (e->ErrorCode);
   }
   return 0;
}
// Displays sending with a connected socket
// using the overload that takes a buffer, message size, and socket flags.
public static int SendReceiveTest3(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int i = server.Send(msg, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", i);
        
        // Get reply from the server.
        int byteCount = server.Receive(bytes, server.Available, 
                                           SocketFlags.None);
        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer, message size, and socket flags.
Public Shared Function SendReceiveTest3(ByVal server As Socket) As Integer 
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
    Try
        ' Blocks until send returns.
        Dim i As Integer = server.Send(msg, msg.Length, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", i)
        
        ' Get reply from the server.
        Dim byteCount As Integer = server.Receive(bytes, server.Available, SocketFlags.None)
        If byteCount > 0 Then
            Console.WriteLine(Encoding.UTF8.GetString(bytes))
        End If
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest3

注解

Send将数据同步发送到ConnectAccept方法中建立的远程主机, 并返回成功发送的字节数。Send synchronously sends data to the remote host established in the Connect or Accept method and returns the number of bytes successfully sent. Send可用于面向连接的协议和无连接的协议。Send can be used for both connection-oriented and connectionless protocols.

此重载需要包含要发送的数据的缓冲区、要发送的字节数以及任意SocketFlags的按位组合。This overload requires a buffer that contains the data you want to send, the number of bytes you want to send, and a bitwise combination of any SocketFlags. 如果指定DontRoute标志socketflags作为参数, 则不会路由要发送的数据。If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

如果使用的是无连接协议, 则必须在Connect调用此方法之前调用, Send否则将引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用的是无连接协议并计划将数据发送到多个不同的主机, 则应SendTo使用方法。If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. 如果不使用SendTo方法, 则必须在每次调用Send方法之前Connect调用方法。If you do not use the SendTo method, you will have to call the Connect method before each call to the Send method. 即使已使用SendTo建立默认远程Connect主机, 也可以使用。You can use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

使用面向连接的协议Send时, 将一直阻止到发送请求的字节数, 除非使用Socket.SendTimeout设置了超时值。With a connection-oriented protocol, Send will block until the requested number of bytes are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式Send下, 即使发送的字节数少于你请求的字节数, 也可能成功完成。In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes you request. 应用程序负责跟踪发送的字节数, 并重试操作, 直到应用程序发送请求的字节数。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 outgoing data is collected. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

必须确保大小不超过基础服务提供程序的最大数据包大小。You must ensure that the size does not exceed the maximum packet size of the underlying service provider. 如果是这样, 则不会发送数据报并SendSocketException引发。If it does, the datagram will not be sent and Send 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.

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError) Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError) Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Net.Sockets.Socket.Send(Byte[], Int32, SocketFlags)

使用指定的 Socket,将列表中的一组缓冲区发送到连接的 SocketFlagsSends the set of buffers in the list to a connected Socket, using the specified SocketFlags.

public:
 int Send(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
[System.CLSCompliant(false)]
public int Send (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Send : System.Collections.Generic.IList<ArraySegment<byte>> * System.Net.Sockets.SocketFlags *  -> int

参数

buffers
IList<ArraySegment<Byte>>

ArraySegment<T> 类型的 Byte 的列表,它包含要发送的数据。A list of ArraySegment<T>s of type Byte that contains the data to be sent.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

errorCode
SocketError SocketError SocketError SocketError

一个 SocketError 对象,它存储套接字错误。A SocketError object that stores the socket error.

返回

发送到 Socket 的字节数。The number of bytes sent to the Socket.

异常

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

注解

此重载至少需要一个包含要发送的数据的缓冲区。This overload requires at least one buffer that contains the data you want to send. SocketFlags值默认为0。The SocketFlags value 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调用此方法之前调用, Send否则将引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method, or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用的是无连接协议并计划将数据发送到多个不同的主机, 则应SendTo使用方法。If you are using a connectionless protocol and plan to send data to several different hosts, you should use the SendTo method. 如果不使用SendTo方法, 则必须在每次调用Send前调用ConnectIf you do not use the SendTo method, you will have to call Connect before each call to Send. 即使已使用SendTo建立默认远程Connect主机, 也可以使用。You can use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议, Send则会一直阻止, 直到发送了缓冲区中的所有字节, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until all of the bytes in the buffer are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式下, Send即使发送的字节数小于缓冲区中的字节数, 也可能成功完成。In non-blocking mode, Send may complete successfully even if it sends less than the number of bytes in the 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 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. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

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

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

Send(ReadOnlySpan<Byte>, SocketFlags, SocketError) Send(ReadOnlySpan<Byte>, SocketFlags, SocketError) Send(ReadOnlySpan<Byte>, SocketFlags, SocketError)

public:
 int Send(ReadOnlySpan<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Send (ReadOnlySpan<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Send : ReadOnlySpan<byte> * System.Net.Sockets.SocketFlags *  -> int

参数

buffer
ReadOnlySpan<Byte>

返回

Send(Byte[], Int32, Int32, SocketFlags) Send(Byte[], Int32, Int32, SocketFlags) Send(Byte[], Int32, Int32, SocketFlags)

从指定偏移量开始,使用指定 SocketFlags,将指定的数据字节数发送到已连接的 SocketSends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags.

public:
 int Send(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags);
public int Send (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags);
member this.Send : byte[] * int * int * System.Net.Sockets.SocketFlags -> 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.

返回

发送到 Socket 的字节数。The number of bytes sent to 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.

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

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

示例

下面的代码示例指定了数据缓冲区、偏移量、大小, 以及SocketFlags用于将数据发送到连接Socket的。The following code example specifies the data buffer, an offset, a size, and SocketFlags for sending data to a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
int SendReceiveTest4( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      
      // Blocks until send returns.
      int byteCount = server->Send( msg, 0, msg->Length, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes, 0, server->Available,
         SocketFlags::None );
      if ( byteCount > 0 )
      {
         Console::WriteLine( Encoding::UTF8->GetString( bytes ) );
      }
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine(  "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
      return (e->ErrorCode);
   }
   return 0;
}
// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
public static int SendReceiveTest4(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);
        
        // Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, 
                                   SocketFlags.None);

        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer, offset, message size, and socket flags.
Public Shared Function SendReceiveTest4(ByVal server As Socket) As Integer 
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
    Try
        ' Blocks until send returns.
        Dim byteCount As Integer = server.Send(msg, 0, msg.Length, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", byteCount)
        
        ' Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, SocketFlags.None)
        
        If byteCount > 0 Then
            Console.WriteLine(Encoding.UTF8.GetString(bytes))
        End If
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest4

注解

Send将数据同步发送到ConnectAccept方法中指定的远程主机, 并返回成功发送的字节数。Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send可用于面向连接的协议和无连接的协议。Send can be used for both connection-oriented and connectionless protocols.

在此重载中, 如果指定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调用此方法之前调用Send , 否则将SocketException引发。If you are using a connectionless protocol, you must call Connect before calling this method or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果你使用的是无连接协议并且打算将数据发送到多个不同的主机, SendTo则应使用。If you are using a connectionless protocol and plan to send data to several different hosts, you should use SendTo. 如果不使用SendTo, 则必须在每次调用SendConnect调用。If you do not use SendTo, you will have to call Connect before each call to Send. 即使已使用建立默认SendTo远程Connect主机, 也可以使用。It is okay to use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

您还必须确保大小不超过基础服务提供程序的最大数据包大小。You must also be sure that the size does not exceed the maximum packet size of the underlying service provider. 如果是这样, 则不会发送数据报并SendSocketException引发。If it does, the datagram will not be sent and Send will throw a SocketException.

如果使用面向连接的协议, Send则会一直阻止到发送请求的字节数, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until the requested number of bytes are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式Send下, 即使发送的字节数少于你请求的字节数, 也可能成功完成。In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes you request. 应用程序负责跟踪发送的字节数, 并重试操作, 直到应用程序发送请求的字节数。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 outgoing data is collected. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

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

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

Send(Byte[], Int32, Int32, SocketFlags, SocketError) Send(Byte[], Int32, Int32, SocketFlags, SocketError) Send(Byte[], Int32, Int32, SocketFlags, SocketError)

从指定的偏移量开始使用指定的 Socket 将指定字节数的数据发送到连接的 SocketFlagsSends the specified number of bytes of data to a connected Socket, starting at the specified offset, and using the specified SocketFlags

public:
 int Send(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int Send (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Send : byte[] * int * int * System.Net.Sockets.SocketFlags *  -> 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.

errorCode
SocketError SocketError SocketError SocketError

一个 SocketError 对象,它存储套接字错误。A SocketError object that stores the socket error.

返回

发送到 Socket 的字节数。The number of bytes sent to 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.

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

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

示例

下面的代码示例指定了数据缓冲区、偏移量、大小, 以及SocketFlags用于将数据发送到连接Socket的。The following code example specifies the data buffer, an offset, a size, and SocketFlags for sending data to a connected Socket.

// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
int SendReceiveTest4( Socket^ server )
{
   array<Byte>^ msg = Encoding::UTF8->GetBytes( "This is a test" );
   array<Byte>^ bytes = gcnew array<Byte>(256);
   try
   {
      
      // Blocks until send returns.
      int byteCount = server->Send( msg, 0, msg->Length, SocketFlags::None );
      Console::WriteLine( "Sent {0} bytes.", byteCount.ToString() );
      
      // Get reply from the server.
      byteCount = server->Receive( bytes, 0, server->Available,
         SocketFlags::None );
      if ( byteCount > 0 )
      {
         Console::WriteLine( Encoding::UTF8->GetString( bytes ) );
      }
   }
   catch ( SocketException^ e ) 
   {
      Console::WriteLine(  "{0} Error code: {1}.", e->Message, e->ErrorCode.ToString() );
      return (e->ErrorCode);
   }
   return 0;
}
// Displays sending with a connected socket
// using the overload that takes a buffer, offset, message size, and socket flags.
public static int SendReceiveTest4(Socket server)
{
    byte[] msg = Encoding.UTF8.GetBytes("This is a test");
    byte[] bytes = new byte[256];
    try 
    {
        // Blocks until send returns.
        int byteCount = server.Send(msg, 0, msg.Length, SocketFlags.None);
        Console.WriteLine("Sent {0} bytes.", byteCount);
        
        // Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, 
                                   SocketFlags.None);

        if (byteCount > 0)
            Console.WriteLine(Encoding.UTF8.GetString(bytes));
    }
    catch (SocketException e)
    {
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
        return (e.ErrorCode);
    }
    return 0;
}
' Displays sending with a connected socket
' using the overload that takes a buffer, offset, message size, and socket flags.
Public Shared Function SendReceiveTest4(ByVal server As Socket) As Integer 
    Dim msg As Byte() = Encoding.UTF8.GetBytes("This is a test")
    Dim bytes(255) As Byte
    Try
        ' Blocks until send returns.
        Dim byteCount As Integer = server.Send(msg, 0, msg.Length, SocketFlags.None)
        Console.WriteLine("Sent {0} bytes.", byteCount)
        
        ' Get reply from the server.
        byteCount = server.Receive(bytes, 0, server.Available, SocketFlags.None)
        
        If byteCount > 0 Then
            Console.WriteLine(Encoding.UTF8.GetString(bytes))
        End If
    Catch e As SocketException
        Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode)
        Return e.ErrorCode
    End Try
    Return 0

End Function 'SendReceiveTest4

注解

Send将数据同步发送到ConnectAccept方法中指定的远程主机, 并返回成功发送的字节数。Send synchronously sends data to the remote host specified in the Connect or Accept method and returns the number of bytes successfully sent. Send可用于面向连接的协议和无连接的协议。Send can be used for both connection-oriented and connectionless protocols.

在此重载中, 如果指定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调用此方法之前调用Send , 否则将SocketException引发。If you are using a connectionless protocol, you must call Connect before calling this method or Send will throw a SocketException. 如果使用面向连接的协议, 则必须使用Connect建立远程主机连接, 或使用Accept来接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果你使用的是无连接协议并且打算将数据发送到多个不同的主机, SendTo则应使用。If you are using a connectionless protocol and plan to send data to several different hosts, you should use SendTo. 如果不使用SendTo, 则必须在每次调用SendConnect调用。If you do not use SendTo, you will have to call Connect before each call to Send. 即使已使用建立默认SendTo远程Connect主机, 也可以使用。It is okay to use SendTo even after you have established a default remote host with Connect. 你还可以Send通过调用来Connect更改默认远程主机, 然后再调用。You can also change the default remote host prior to calling Send by making another call to Connect.

您还必须确保大小不超过基础服务提供程序的最大数据包大小。You must also be sure that the size does not exceed the maximum packet size of the underlying service provider. 如果是这样, 则不会发送数据报并SendSocketException引发。If it does, the datagram will not be sent and Send will throw a SocketException.

如果使用面向连接的协议, Send则会一直阻止到发送请求的字节数, 除非使用Socket.SendTimeout设置了超时。If you are using a connection-oriented protocol, Send will block until the requested number of bytes are sent, unless a time-out was set by using Socket.SendTimeout. 如果超过超时值, 则Send调用将SocketException引发。If the time-out value was exceeded, the Send call will throw a SocketException. 在非阻止模式Send下, 即使发送的字节数少于你请求的字节数, 也可能成功完成。In nonblocking mode, Send may complete successfully even if it sends less than the number of bytes you request. 应用程序负责跟踪发送的字节数, 并重试操作, 直到应用程序发送请求的字节数。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 outgoing data is collected. Send方法成功完成意味着基础系统具有空间来缓冲用于网络发送的数据。A successful completion of the Send method means that the underlying system has had room to buffer your data for a network send.

备注

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

备注

成功完成发送并不表示数据已成功传递。The successful completion of a send does not indicate that the data was successfully delivered. 如果传输系统中没有可用于保存要传输的数据的缓冲区空间, 则发送将会阻止, 除非已将套接字置于非阻止模式。If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

适用于