Will Socket.SendAsync send everything in 1 call? #100436
-
Socket.SendAsync() returns an int, does this mean we have to loop in case it doesn't send everything in one call, or is it guaranteed to send the entire buffer? Is this safe? ReadOnlyMemory<byte> buffer = ...
await socket.SendAsync(buffer); or is this required? ReadOnlyMemory<byte> buffer = ...
while (buffer.Length > 0)
{
var written = await socket.SendAsync(buffer);
buffer = buffer[written..];
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Per the documentation:
Yes, the loop will be required. |
Beta Was this translation helpful? Give feedback.
-
We should update the docs. As long as the Socket.Blocking is true, for a connection-oriented protocol (e.g. TCP), Socket.SendAsync will ensure all the data has been sent (from the perspective of the application) and you do not need a loop. This is why NetworkStream.WriteAsync doesn't have or need a loop. On Windows, the implementation uses WSASend, which makes this guarantee. And on Unix, the implementation has its own loop to provide a similar guarantee. |
Beta Was this translation helpful? Give feedback.
We should update the docs. As long as the Socket.Blocking is true, for a connection-oriented protocol (e.g. TCP), Socket.SendAsync will ensure all the data has been sent (from the perspective of the application) and you do not need a loop. This is why NetworkStream.WriteAsync doesn't have or need a loop.
On Windows, the implementation uses WSASend, which makes this guarantee. And on Unix, the implementation has its own loop to provide a similar guarantee.