In the following C# .NET8 snippet, should the following ValueTask Socket.SendAsync() call return immediately or only after the transmission is complete? On my system, the code snippet below returns from soc.SendAsync() immediately while the transmission continues in the background (verified by observing Wireshark traffic). I did not expect this, and I don’t see a lot of examples using SendAsync() that returns ValueTask.
What alternatives exists so the send method returns only after the transmission is complete?
using System.Net.Sockets;
private static async Task Main(string[] args)
{
using Socket soc = new(SocketType.Stream, ProtocolType.Tcp);
await soc.ConnectAsync("127.0.0.2", 10001);
byte[] byteArray = new byte[100_000];
var mem = new ReadOnlyMemory<byte>(byteArray);
// This SendAsync call returns immediately even though transmission continues in background.
// Should this be awaited before returning bytesSent?
int bytesSent = await soc.SendAsync(mem, SocketFlags.None);
}
4