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方法将数据读入缓冲区参数并返回已成功读取的字节数。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以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取数据中建立的远程主机发送过来ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用将引发SocketExceptionIf the time-out value was exceeded, the Receive call will throw a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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方法将读取所有可用的最多指定大小参数的字节数的数量的数据。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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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.

示例

下面的代码示例上连接接收数据之前指定数据缓冲区、 偏移、 大小和套接字标志SocketThe 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方法将数据读入缓冲区参数并返回已成功读取的字节数。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以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取数据中建立的远程主机发送过来ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用将引发SocketExceptionIf the time-out value was exceeded, the Receive call will throw a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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方法将读取所有可用的最多指定大小参数的字节数的数量的数据。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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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. SocketFlags默认值为NoneThe SocketFlags value defaults to None.

如果使用的是面向连接的协议,必须调用Connect建立的远程主机连接,或Accept以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取来自中建立的远程主机连接的数据ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用,则会引发SocketExceptionIf the time-out value was exceeded, the Receive call throws a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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,并指定NoneSocketFlagsThe 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.

此重载仅要求您提供接收缓冲区中,你想要接收的字节数和所需的数量SocketFlagsThis overload only requires you to provide a receive buffer, the number of bytes you want to receive, and the necessary SocketFlags.

如果使用的是面向连接的协议,必须调用Connect建立的远程主机连接,或Accept以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取数据中建立的远程主机发送过来ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用将引发SocketExceptionIf the time-out value was exceeded, the Receive call will throw a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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[], 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上连接接收数据的SocketThe 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方法将数据读入缓冲区参数并返回已成功读取的字节数。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.

此重载仅要求您提供接收缓冲区和所需SocketFlagsThis overload only requires you to provide a receive buffer and the necessary SocketFlags. 缓冲区偏移量的默认值为 0,并且大小默认为字节参数的长度。The buffer offset defaults to 0, and the size defaults to the length of the byte parameter.

如果使用的是面向连接的协议,必须调用Connect建立的远程主机连接,或Accept以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取数据中建立的远程主机发送过来ConnectAccept方法。The 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方法将立即完成并引发SocketExceptionIf 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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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. 关联枚举:AcceptAssociated 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以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取来自中建立的远程主机连接的数据ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用将引发SocketExceptionIf the time-out value was exceeded, the Receive call will throw a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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.

示例

下面的代码示例接收上连接的数据SocketThe 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方法将数据读入缓冲区参数并返回已成功读取的字节数。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,默认大小为缓冲区参数的长度并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以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取数据中建立的远程主机发送过来ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用将引发SocketExceptionIf the time-out value was exceeded, the Receive call will throw a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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.

示例

下面的代码示例演示如何在连接上接收数据SocketThe 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. SocketFlags默认值为NoneThe SocketFlags value defaults to None.

如果使用的是面向连接的协议,必须调用Connect建立的远程主机连接,或Accept以接受传入连接之前调用ReceiveIf 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. Receive方法将只读取来自中建立的远程主机连接的数据ConnectAccept方法。The 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.ReceiveTimeoutIf 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调用,则会引发SocketExceptionIf the time-out value was exceeded, the Receive call throws a SocketException. 如果您是在非阻止模式下,并且没有在可用的数据中的协议堆栈缓冲区Receive方法将立即完成并引发SocketExceptionIf 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.

如果您使用的是无连接SocketReceive会从在指定的目标地址中读取第一个排队数据报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.

另请参阅

适用于