The docs for Socket.SendFile, as well as the nullability annotations for the multi-arg overload, allow for SendFile to be passed a null file path, e.g. this succeeds on .NET Framework and on Linux/macOS on .NET Core:
usingSystem.Net;usingSystem.Net.Sockets;classProgram{staticvoidMain(){varclient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);varlistener=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);listener.Bind(newIPEndPoint(IPAddress.Loopback,0));listener.Listen(1);client.Connect(listener.LocalEndPoint);varserver=listener.Accept();varbuffer=newbyte[1_000_000];client.SendFile(null,buffer,buffer,TransmitFileOptions.UseDefaultWorkerThread);intcount=0;while(count<buffer.Length*2){intreceived=server.Receive(buffer);count+=received;}}}On .NET Core (both 3.1 and 5) on Windows, however, this throws:
Unhandled exception. System.ArgumentNullException: SafeHandle cannot be null. (Parameter 'pHandle')
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Mswsock.TransmitFile(SafeHandle socket, SafeHandle fileHandle, Int32 numberOfBytesToWrite, Int32 numberOfBytesPerSend, NativeOverlapped* overlapped, TransmitFileBuffers* buffers, TransmitFileOptions flags)
at System.Net.Sockets.SocketPal.TransmitFileHelper(SafeHandle socket, SafeHandle fileHandle, NativeOverlapped* overlapped, Byte[] preBuffer, Byte[] postBuffer, TransmitFileOptions flags)
at System.Net.Sockets.SocketPal.SendFile(SafeSocketHandle handle, SafeFileHandle fileHandle, Byte[] preBuffer, Byte[] postBuffer, TransmitFileOptions flags)
at System.Net.Sockets.Socket.SendFileInternal(String fileName, Byte[] preBuffer, Byte[] postBuffer, TransmitFileOptions flags)
at System.Net.Sockets.Socket.SendFile(String fileName, Byte[] preBuffer, Byte[] postBuffer, TransmitFileOptions flags)
at System.Net.Sockets.Socket.SendFile(String fileName)
at Program.Main()
The docs for Socket.SendFile, as well as the nullability annotations for the multi-arg overload, allow for SendFile to be passed a null file path, e.g. this succeeds on .NET Framework and on Linux/macOS on .NET Core:
On .NET Core (both 3.1 and 5) on Windows, however, this throws: