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.

示例

下面的代码示例演示如何在连接上的发送数据SocketThe 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方法,您将需要致电Connect每次调用前SendIf you do not use the SendTo method, you will have to call Connect before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectYou can use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议Send会阻塞,直到所有缓冲区中的字节都将发送,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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方法,您将需要致电Connect每次调用前SendIf you do not use the SendTo method, you will have to call Connect before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectYou can use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议Send会阻塞,直到所有缓冲区中的字节都将发送,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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.

示例

下面的代码示例演示如何在连接上的发送数据SocketThe 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.

此重载需要包含想要发送的数据的缓冲区和的按位组合SocketFlagsThis 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方法,然后再每次调用SendIf you do not use the SendTo method, you will have to call the Connect method before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectYou can use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议Send会阻塞,直到所有缓冲区中的字节都将发送,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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. 如果是这样,将不发送数据报并Send将引发SocketExceptionIf 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方法,您将需要致电Connect每次调用前SendIf you do not use the SendTo method, you will have to call Connect before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectYou can use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议Send会阻塞,直到所有缓冲区中的字节都将发送,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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.

示例

下面的代码示例将发送缓冲区中找到的数据,并指定NoneSocketFlagsThe 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.

此重载需要包含想要你想要发送的字节数和按位组合的任何发送的数据的缓冲区SocketFlagsThis 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方法,您将需要致电Connect方法,然后再每次调用Send方法。If you do not use the SendTo method, you will have to call the Connect method before each call to the Send method. 可以使用SendTo甚至已建立默认远程主机使用后ConnectYou can use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou can also change the default remote host prior to calling Send by making another call to Connect.

使用面向连接的协议Send会阻塞,直到发送请求的字节数,除非使用设置超时Socket.SendTimeoutWith 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调用将引发SocketExceptionIf 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. 如果是这样,将不发送数据报并Send将引发SocketExceptionIf 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方法,您将需要致电Connect每次调用前SendIf you do not use the SendTo method, you will have to call Connect before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectYou can use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou can also change the default remote host prior to calling Send by making another call to Connect.

如果使用面向连接的协议Send会阻塞,直到所有缓冲区中的字节都将发送,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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将数据发送到连接SocketThe 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将引发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.

如果使用无连接协议,并且打算将数据发送到多个不同的主机,则应使用SendToIf you are using a connectionless protocol and plan to send data to several different hosts, you should use SendTo. 如果不使用SendTo,您将需要致电Connect每次调用前SendIf you do not use SendTo, you will have to call Connect before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectIt is okay to use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou 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. 如果是这样,将不发送数据报并Send将引发SocketExceptionIf it does, the datagram will not be sent and Send will throw a SocketException.

如果使用面向连接的协议Send会阻塞,直到发送请求的字节数,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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将数据发送到连接SocketThe 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将引发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.

如果使用无连接协议,并且打算将数据发送到多个不同的主机,则应使用SendToIf you are using a connectionless protocol and plan to send data to several different hosts, you should use SendTo. 如果不使用SendTo,您将需要致电Connect每次调用前SendIf you do not use SendTo, you will have to call Connect before each call to Send. 可以使用SendTo甚至已建立默认远程主机使用后ConnectIt is okay to use SendTo even after you have established a default remote host with Connect. 您还可以更改默认远程主机之前调用Send到另一个调用,从而ConnectYou 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. 如果是这样,将不发送数据报并Send将引发SocketExceptionIf it does, the datagram will not be sent and Send will throw a SocketException.

如果使用面向连接的协议Send会阻塞,直到发送请求的字节数,除非使用设置超时Socket.SendTimeoutIf 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调用将引发SocketExceptionIf 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.

另请参阅

适用于