I'm occasionally getting the following exception when I call udpClient.SendAsync. It's maybe in 10% of the cases:
SocketException: An address incompatible with the requested protocol was used.
Full code:
public static UdpClient Create()
{
var client = new UdpClient();
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;
// client.EnableBroadcast = true; // adding this does not help either.
return client;
}
using var udpClient = Create();
_ = await udpClient.SendAsync(datagram, datagram.Length, new IPEndPoint(IPAddress.Broadcast, 55555))
.ConfigureAwait(false);
I've seen other questions on StackOverflow about the same exception, but that's pretty much all code that creates socket instances, which is not the case here.