Socket.SendFile() is not supported on Windows, but the exceptions being thrown are not consistent. The first call throws SocketException, while the second attempt leads to NotSupportedException.
Repro
The twice=true case fails:
[Theory]
[InlineData(false)]
[InlineData(true)]
public void UDP_SendFile(bool twice)
{
string tempFile = Path.GetTempFileName();
File.WriteAllBytes(tempFile, new byte[128]);
using var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
listener.BindToAnonymousPort(IPAddress.Loopback);
client.Connect(listener.LocalEndPoint);
Assert.Throws<SocketException>(() => client.SendFile(tempFile));
if (twice)
{
Assert.Throws<SocketException>(() => client.SendFile(tempFile)); // Throws NotSupportedException
}
}
Edit (after discussion below)
On Linux the call does not fail. We should fail NotSupportedException for connectionless sockets on all OS-es in all cases.
Socket.SendFile()is not supported on Windows, but the exceptions being thrown are not consistent. The first call throwsSocketException, while the second attempt leads toNotSupportedException.Repro
The
twice=truecase fails:Edit (after discussion below)
On Linux the call does not fail. We should fail
NotSupportedExceptionfor connectionless sockets on all OS-es in all cases.