diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index 9631bb66e8ca5e..3e7f051a18036b 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -677,8 +677,8 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory preBuffer, if (!IsConnectionOriented) { - var soex = new SocketException((int)SocketError.NotConnected); - return ValueTask.FromException(soex); + var ex = new NotSupportedException(SR.net_notconnected); + return ValueTask.FromException(ex); } int packetsCount = 0; diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 03b6a12b440e73..9c9afb9ffd4241 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -1247,7 +1247,7 @@ public void SendFile(string? fileName, ReadOnlySpan preBuffer, ReadOnlySpa { ThrowIfDisposed(); - if (!Connected) + if (!IsConnectionOriented || !Connected) { throw new NotSupportedException(SR.net_notconnected); } diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs index e3d35f285e0630..7a8761397fbf56 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs @@ -62,7 +62,6 @@ public async Task FileDoesNotExist_ThrowsFileNotFoundException(bool useOverloadW [Theory] [InlineData(false)] [InlineData(true)] - [PlatformSpecific(TestPlatforms.Windows)] public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload) { // Create file to send @@ -77,16 +76,14 @@ public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload client.Connect(listener.LocalEndPoint); - SocketException ex; if (usePreAndPostbufferOverload) { - ex = await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path, Array.Empty(), Array.Empty(), TransmitFileOptions.UseDefaultWorkerThread)); + await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path, Array.Empty(), Array.Empty(), TransmitFileOptions.UseDefaultWorkerThread)); } else { - ex = await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path)); + await Assert.ThrowsAsync(() => SendFileAsync(client, tempFile.Path)); } - Assert.Equal(SocketError.NotConnected, ex.SocketErrorCode); } public static IEnumerable SendFile_MemberData()