#unity/日常积累 # Socket.ReceiveAsync 方法 - 参考 反馈 [](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket.receiveasync?view=net-8.0&redirectedfrom=MSDN#definition) ## 定义 命名空间: [System.Net.Sockets](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets?view=net-8.0) 程序集: System.Net.Sockets.dll ## ``` ReceiveAsync(ArraySegment) ``` Source: [Socket.Tasks.cs](https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs#L277C13-L277C51) 接收连接的套接字发出的数据。 ``` cs public System.Threading.Tasks.Task ReceiveAsync (ArraySegment buffer); ``` #### 参数 buffer [ArraySegment](https://learn.microsoft.com/zh-cn/dotnet/api/system.arraysegment-1?view=net-8.0)<[Byte](https://learn.microsoft.com/zh-cn/dotnet/api/system.byte?view=net-8.0)> 接收的数据的缓冲区。 #### 返回 [Task](https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.task-1?view=net-8.0)<[Int32](https://learn.microsoft.com/zh-cn/dotnet/api/system.int32?view=net-8.0)> 以收到的字节数完成的异步任务。 ## ``` ReceiveAsync(IList>) ``` Source: [Socket.Tasks.cs](https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs#L336C13-L336C52) 接收连接的套接字发出的数据。 ``` cs public System.Threading.Tasks.Task ReceiveAsync (System.Collections.Generic.IList> buffers); ``` #### 参数 buffers [IList](https://learn.microsoft.com/zh-cn/dotnet/api/system.collections.generic.ilist-1?view=net-8.0)<[ArraySegment](https://learn.microsoft.com/zh-cn/dotnet/api/system.arraysegment-1?view=net-8.0)<[Byte](https://learn.microsoft.com/zh-cn/dotnet/api/system.byte?view=net-8.0)>> 接收的数据的缓冲区列表。 #### 返回 [Task](https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.task-1?view=net-8.0)<[Int32](https://learn.microsoft.com/zh-cn/dotnet/api/system.int32?view=net-8.0)> 以收到的字节数完成的异步任务。 #### 例外 [ArgumentNullException](https://learn.microsoft.com/zh-cn/dotnet/api/system.argumentnullexception?view=net-8.0) 参数 `buffers` 为 null。 [ObjectDisposedException](https://learn.microsoft.com/zh-cn/dotnet/api/system.objectdisposedexception?view=net-8.0) [Socket](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?view=net-8.0) 已关闭。 [SocketException](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socketexception?view=net-8.0) 尝试访问套接字时出错。 ### 注解 此方法存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 [ArgumentException](https://learn.microsoft.com/zh-cn/dotnet/api/system.argumentexception?view=net-8.0))仍会同步引发。 有关存储的异常,请参阅 引发的 [Receive(IList>)](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket.receive?view=net-8.0#system-net-sockets-socket-receive(system-collections-generic-ilist((system-arraysegment((system-byte))))))异常。