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 a04605b94fd38..e1891bef916f4 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 @@ -749,8 +749,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 b918cebb84058..414b0baa86e68 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 @@ -1258,7 +1258,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 e3d35f285e063..7a8761397fbf5 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()