#unity/日常积累 # Socket.SendAsync 方法 - 参考 反馈 [](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket.sendasync?view=net-8.0#definition) ## 定义 命名空间: [System.Net.Sockets](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets?view=net-8.0) 程序集: System.Net.Sockets.dll 在连接的套接字上发送数据。 ``` cs public System.Threading.Tasks.ValueTask SendAsync (ReadOnlyMemory buffer, System.Threading.CancellationToken cancellationToken = default); ``` #### 参数 buffer [ReadOnlyMemory](https://learn.microsoft.com/zh-cn/dotnet/api/system.readonlymemory-1?view=net-8.0)<[Byte](https://learn.microsoft.com/zh-cn/dotnet/api/system.byte?view=net-8.0)> 要发送的数据的缓冲区。 cancellationToken [CancellationToken](https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.cancellationtoken?view=net-8.0) 可用于取消异步操作的取消标记。 #### 返回 [ValueTask](https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.valuetask-1?view=net-8.0)<[Int32](https://learn.microsoft.com/zh-cn/dotnet/api/system.int32?view=net-8.0)> 以发送的字节数完成的异步任务。 #### 例外 [OperationCanceledException](https://learn.microsoft.com/zh-cn/dotnet/api/system.operationcanceledexception?view=net-8.0) 取消令牌已取消。 此异常存储在返回的任务中。 ### 注解 此方法将存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 [ArgumentException](https://learn.microsoft.com/zh-cn/dotnet/api/system.argumentexception?view=net-8.0))仍会同步引发。 有关存储的异常,请参阅 引发的 [Send(IList>, SocketFlags)](https://learn.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket.send?view=net-8.0#system-net-sockets-socket-send(system-collections-generic-ilist((system-arraysegment((system-byte))))-system-net-sockets-socketflags))异常。