Socket.Receive Socket.Receive Socket.Receive Socket.Receive Method

定义

接收来自绑定的 Socket 的数据。Receives data from a bound Socket.

重载

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

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区。Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

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

使用指定的 Socket,从绑定的 SocketFlags 接收指定的字节数,存入接收缓冲区的指定偏移量位置。Receives the specified number of bytes from a bound Socket into the specified offset position of the receive buffer, using the specified SocketFlags.

Receive(Span<Byte>, SocketFlags, SocketError) Receive(Span<Byte>, SocketFlags, SocketError) Receive(Span<Byte>, SocketFlags, SocketError)
Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError) Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError) Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError)

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区列表中。Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

Receive(Span<Byte>, SocketFlags) Receive(Span<Byte>, SocketFlags) Receive(Span<Byte>, SocketFlags)
Receive(Byte[], Int32, SocketFlags) Receive(Byte[], Int32, SocketFlags) Receive(Byte[], Int32, SocketFlags)

使用指定的 Socket,从绑定的 SocketFlags 接收指定字节数的数据,并将数据存入接收缓冲区。Receives the specified number of bytes of data from a bound Socket into a receive buffer, using the specified SocketFlags.

Receive(Byte[], SocketFlags) Receive(Byte[], SocketFlags) Receive(Byte[], SocketFlags)

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区。Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

Receive(Span<Byte>) Receive(Span<Byte>) Receive(Span<Byte>) Receive(Span<Byte>)
Receive(IList<ArraySegment<Byte>>) Receive(IList<ArraySegment<Byte>>) Receive(IList<ArraySegment<Byte>>) Receive(IList<ArraySegment<Byte>>)

从绑定的 Socket 接收数据,将数据存入接收缓冲区列表中。Receives data from a bound Socket into the list of receive buffers.

Receive(Byte[]) Receive(Byte[]) Receive(Byte[]) Receive(Byte[])

从绑定的 Socket 套接字接收数据,将数据存入接收缓冲区。Receives data from a bound Socket into a receive buffer.

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

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区列表中。Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

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

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区。Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

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

参数

buffer
Byte[]

Byte 类型的数组,它是存储接收到的数据的位置。An array of type Byte that is the storage location for the received data.

offset
Int32 Int32 Int32 Int32

buffer 参数中的位置,用于存储所接收的数据。The position in the buffer parameter to store the received data.

size
Int32 Int32 Int32 Int32

要接收的字节数。The number of bytes to receive.

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.

返回

收到的字节数。The number of bytes received.

异常

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- 未设置 LocalEndPoint 属性。The LocalEndPoint property is not set.

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

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

注解

方法Receive将数据读入 buffer 参数, 并返回成功读取的字节数。The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. 可从面向Receive连接的套接字和无连接套接字调用。You can call Receive from both connection-oriented and connectionless sockets.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超过超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call will throw a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参阅下面的备注。See Remarks below. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 最多可达 size 参数指定的字节数。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the number of bytes specified by the size parameter. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffer参数的大小, buffer则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

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

使用指定的 Socket,从绑定的 SocketFlags 接收指定的字节数,存入接收缓冲区的指定偏移量位置。Receives the specified number of bytes from a bound Socket into the specified offset position of the receive buffer, using the specified SocketFlags.

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

参数

buffer
Byte[]

类型 Byte 的数组,它是所接受的数据的存储位置。An array of type Byte that is the storage location for received data.

offset
Int32 Int32 Int32 Int32

buffer 中存储所接收数据的位置。The location in buffer to store the received data.

size
Int32 Int32 Int32 Int32

要接收的字节数。The number of bytes to receive.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

返回

收到的字节数。The number of bytes received.

异常

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- 未设置 LocalEndPoint 属性。The LocalEndPoint property was not set.

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

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

示例

下面的代码示例指定了一个数据缓冲区、偏移量、大小和套接字标志, 然后在连接Socket的上接收数据。The following code example specifies a data buffer, an offset, a size, and a socket flag before receiving data on 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

注解

方法Receive将数据读入 buffer 参数, 并返回成功读取的字节数。The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. 可从面向Receive连接的套接字和无连接套接字调用。You can call Receive from both connection-oriented and connectionless sockets.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超过超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call will throw a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 尝试访问套接字时出错。An error occurred when attempting to access the socket. 请参阅下面的备注。See Remarks below. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 最多可达 size 参数指定的字节数。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the number of bytes specified by the size parameter. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffer参数的大小, buffer则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

Receive(Span<Byte>, SocketFlags, SocketError) Receive(Span<Byte>, SocketFlags, SocketError) Receive(Span<Byte>, SocketFlags, SocketError)

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

参数

buffer
Span<Byte>

返回

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

重要

此 API 不符合 CLS。

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

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区列表中。Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

public:
 int Receive(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 Receive (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode);
member this.Receive : 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 received data.

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.

返回

收到的字节数。The number of bytes received.

异常

buffersnullbuffers is null.

-or- buffers.Count 是零。buffers.Count is zero.

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

注解

此方法将buffers数据读入参数, 并返回成功读取的字节数。This method reads data into the buffers parameter and returns the number of bytes successfully read. 可从面向连接的套接字和无连接套接字调用。You can call from both connection-oriented and connectionless sockets.

此重载要求提供一个或多个接收缓冲区。This overload requires you to provide one or more receive buffers. 值默认为NoneSocketFlagsThe SocketFlags value defaults to None.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机连接到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host connection established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超出了超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call throws a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 直到达到缓冲区的大小。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket,Receive will read the first queued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffers参数的大小, buffers则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

Receive(Span<Byte>, SocketFlags) Receive(Span<Byte>, SocketFlags) Receive(Span<Byte>, SocketFlags)

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

参数

buffer
Span<Byte>

返回

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

使用指定的 Socket,从绑定的 SocketFlags 接收指定字节数的数据,并将数据存入接收缓冲区。Receives the specified number of bytes of data from a bound Socket into a receive buffer, using the specified SocketFlags.

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

参数

buffer
Byte[]

Byte 类型的数组,它是存储接收到的数据的位置。An array of type Byte that is the storage location for the received data.

size
Int32 Int32 Int32 Int32

要接收的字节数。The number of bytes to receive.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

返回

收到的字节数。The number of bytes received.

异常

size 超出了 buffer 的大小。size exceeds the size of buffer.

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

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

示例

下面的将接收找到buffer的数据, 并为None SocketFlags指定。The following receives the data found into buffer, and specifies None for SocketFlags.

// Receive the host home page content and loop until all the data is received.
Int32 bytes = s->Receive( RecvBytes, RecvBytes->Length, SocketFlags::None );
strRetPage =  "Default HTML page on ";
strRetPage->Concat( server,  ":\r\n", ASCII->GetString( RecvBytes, 0, bytes ) );
while ( bytes > 0 )
{
   bytes = s->Receive( RecvBytes, RecvBytes->Length, SocketFlags::None );
   strRetPage->Concat( ASCII->GetString( RecvBytes, 0, bytes ) );
}



// Receive the host home page content and loop until all the data is received.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

while (bytes > 0)
{
  bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
  strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}


' Receive the host home page content and loop until all the data is received.

'Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)

strRetPage = "Default HTML page on " + server + ":\r\n"
strRetPage = "Default HTML page on " + server + ":" + ControlChars.Lf + ControlChars.NewLine

Dim i As Integer

While bytes > 0

  bytes = s.Receive(RecvBytes, RecvBytes.Length, 0)

  strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)

End While

注解

方法Receive将数据buffer读入参数并返回成功读取的字节数。The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. 可从面向Receive连接的套接字和无连接套接字调用。You can call Receive from both connection-oriented and connectionless sockets.

此重载只要求你提供接收缓冲区、要接收的字节数和必要SocketFlags的。This overload only requires you to provide a receive buffer, the number of bytes you want to receive, and the necessary SocketFlags.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超过超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call will throw a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, The Receive method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry your receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 直到size参数指定的字节数。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the number of bytes specified by the size parameter. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffer参数的大小, buffer则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联的枚举Accept:。Associated enumeration: Accept.

另请参阅

Receive(Byte[], SocketFlags) Receive(Byte[], SocketFlags) Receive(Byte[], SocketFlags)

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区。Receives data from a bound Socket into a receive buffer, using the specified SocketFlags.

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

参数

buffer
Byte[]

Byte 类型的数组,它是存储接收到的数据的位置。An array of type Byte that is the storage location for the received data.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

返回

收到的字节数。The number of bytes received.

异常

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

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

示例

下面的代码示例指定了一个数据缓冲区, SocketFlags用于在连接Socket的上接收数据。The following code example specifies a data buffer, and SocketFlags for receiving 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

注解

方法Receive将数据读入 buffer 参数, 并返回成功读取的字节数。The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. 可从面向Receive连接的套接字和无连接套接字调用。You can call Receive from both connection-oriented and connectionless sockets.

此重载只要求你提供接收缓冲区和必要SocketFlags的。This overload only requires you to provide a receive buffer and the necessary SocketFlags. 缓冲区偏移量默认为 0, 大小默认为 byte 参数的长度。The buffer offset defaults to 0, and the size defaults to the length of the byte parameter.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将被阻止, 直到数据可用。If no data is available for reading, the Receive method will block until data is available. 如果处于非阻止模式, 并且协议堆栈缓冲区中没有可用数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry your receive operation.

如果使用面向Socket连接的, 则该Receive方法将读取尽可能多的数据, 直到缓冲区大小可用。If you are using a connection-oriented Socket, the Receive method will read as much data as is available up to the size of the buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffer参数的大小, buffer则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联的枚举Accept:。Associated enumeration: Accept.

另请参阅

Receive(Span<Byte>) Receive(Span<Byte>) Receive(Span<Byte>) Receive(Span<Byte>)

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

参数

buffer
Span<Byte>

返回

Receive(IList<ArraySegment<Byte>>) Receive(IList<ArraySegment<Byte>>) Receive(IList<ArraySegment<Byte>>) Receive(IList<ArraySegment<Byte>>)

从绑定的 Socket 接收数据,将数据存入接收缓冲区列表中。Receives data from a bound Socket into the list of receive buffers.

public:
 int Receive(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers);
public int Receive (System.Collections.Generic.IList<ArraySegment<byte>> buffers);
member this.Receive : System.Collections.Generic.IList<ArraySegment<byte>> -> int
Public Function Receive (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 received data.

返回

收到的字节数。The number of bytes received.

异常

buffer 参数为 nullThe buffer parameter is null.

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

注解

此方法将数据读取到缓冲区参数, 并返回成功读取的字节数。This method reads data into the buffers parameter and returns the number of bytes successfully read. 可从面向连接的套接字和无连接套接字调用。You can call from both connection-oriented and connectionless sockets.

此重载要求提供一个或多个接收缓冲区。This overload requires you to provide one or more receive buffers.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机连接到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host connection established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超过超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call will throw a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 直到达到缓冲区的大小。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffers参数的大小, buffers则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

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

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

Receive(Byte[]) Receive(Byte[]) Receive(Byte[]) Receive(Byte[])

从绑定的 Socket 套接字接收数据,将数据存入接收缓冲区。Receives data from a bound Socket into a receive buffer.

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

参数

buffer
Byte[]

Byte 类型的数组,它是存储接收到的数据的位置。An array of type Byte that is the storage location for the received data.

返回

收到的字节数。The number of bytes received.

异常

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

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

示例

下面的代码示例在连接Socket的上接收数据。The following code example receives 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

注解

方法Receive将数据读入 buffer 参数, 并返回成功读取的字节数。The Receive method reads data into the buffer parameter and returns the number of bytes successfully read. 可从面向Receive连接的套接字和无连接套接字调用。You can call Receive from both connection-oriented and connectionless sockets.

此重载只要求你提供接收缓冲区。This overload only requires you to provide a receive buffer. 缓冲区偏移量默认为 0, 大小默认为 buffer 参数的长度, SocketFlags值默认为。 NoneThe buffer offset defaults to 0, the size defaults to the length of the buffer parameter, and the SocketFlags value defaults to None.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超过超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call will throw a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 直到达到缓冲区的大小。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first queued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffer参数的大小, buffer则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

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

重要

此 API 不符合 CLS。

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

使用指定的 Socket,从绑定的 SocketFlags 接收数据,将数据存入接收缓冲区列表中。Receives data from a bound Socket into the list of receive buffers, using the specified SocketFlags.

public:
 int Receive(System::Collections::Generic::IList<ArraySegment<System::Byte>> ^ buffers, System::Net::Sockets::SocketFlags socketFlags);
[System.CLSCompliant(false)]
public int Receive (System.Collections.Generic.IList<ArraySegment<byte>> buffers, System.Net.Sockets.SocketFlags socketFlags);
member this.Receive : 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 received data.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

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

返回

收到的字节数。The number of bytes received.

异常

buffersnullbuffers is null.

-or- buffers.Count 是零。buffers.Count is zero.

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

示例

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


// Build the buffers for the receive.
List<ArraySegment<Byte> >^ receiveBuffers = 
    gcnew List<ArraySegment<Byte> >(2);

array<Byte>^ bigBuffer = gcnew array<Byte>(1024);

// Specify the first buffer segment (2 bytes, starting 
// at the 4th element of bigBuffer)
receiveBuffers->Add(ArraySegment<Byte>(bigBuffer, 4, 2));

// Specify the second buffer segment (500 bytes, starting
// at the 20th element of bigBuffer)
receiveBuffers->Add(
    ArraySegment<Byte>(bigBuffer, 20, 500));

tcpSocket->Receive(receiveBuffers);

Console::WriteLine("{0}", 
    asciiEncoding->GetString(bigBuffer));

// Build the buffers for the receive.
List<ArraySegment<byte>> recvBuffers = 
                         new List<ArraySegment<byte>>(2);

byte[] bigBuffer = new byte[1024];

// Specify the first buffer segment (2 bytes, starting 
// at the 4th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>
                        (bigBuffer, 4, 2));

// Specify the second buffer segment (500 bytes, starting
// at the 20th element of bigBuffer)
recvBuffers.Add(new ArraySegment<byte>
                        (bigBuffer, 20, 500));

int bytesReceived = mySocket.Receive(recvBuffers);

Console.WriteLine("{0}", ASCII.GetString(bigBuffer));

注解

此方法将buffers数据读入参数, 并返回成功读取的字节数。This method reads data into the buffers parameter and returns the number of bytes successfully read. 可从面向连接的套接字和无连接套接字调用。You can call from both connection-oriented and connectionless sockets.

此重载要求提供一个或多个接收缓冲区。This overload requires you to provide one or more receive buffers. 值默认为NoneSocketFlagsThe SocketFlags value defaults to None.

如果使用面向连接的协议, 则必须调用Connect以建立远程主机连接, 或Accept在调用Receive之前接受传入连接。If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. 方法只读取从ConnectAccept方法中建立的远程主机连接到达的数据。 ReceiveThe Receive method will only read data that arrives from the remote host connection established in the Connect or Accept method. 如果你使用的是无连接协议, 则还可以使用ReceiveFrom方法。If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom将允许接收来自任何主机的数据。ReceiveFrom will allow you to receive data arriving from any host.

如果没有可供读取的数据, 则Receive该方法将在数据可用之前被阻止, 除非使用Socket.ReceiveTimeout设置超时值。If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. 如果超出了超时值, 则Receive调用将SocketException引发。If the time-out value was exceeded, the Receive call throws a SocketException. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该Receive方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

如果使用面向Socket连接的, 该Receive方法将读取尽可能多的数据, 直到达到缓冲区的大小。If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该Receive方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

如果你使用的是无Socket连接Receive , 将从你在Connect方法中指定的目标地址读取第一个排队的数据报。If you are using a connectionless Socket, Receive will read the first enqueued datagram from the destination address you specify in the Connect method. 如果接收的数据报大于buffers参数的大小, buffers则将使用消息的第一部分进行填充, 超过的SocketException数据将会丢失, 并引发。If the datagram you receive is larger than the size of the buffers parameter, buffers gets filled with the first part of the message, the excess data is lost and a SocketException is thrown.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

适用于