Skip to content

Commit 24f1513

Browse files
rhirano0715karelz
andauthored
Unified to throw NotSupportedException when SendFile() for connectionless sockets (#87108)
* Unified to throw NotSupportedException when SendFile() for connectionless sockets The issue was that the Socket.SendFile() threw inconsistent exceptions when the platform was Windows and the protocol was UDP. The first call would throw a SocketException, while the second call would throw a NotSupportedException. With this commit, SendFile() will consistently throw NotSupportException on all platforms when the protocol is UDP. Fix #47472 * Change to throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`. Before:. Throws `NotSupportedException` on UDP. After: Throws `NotSupportedException` if `!IsConnectionOriented` or `!Connected`. * Changed test case `UdpConnection_ThrowsException` to run regardless of platform. * Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs Co-authored-by: Karel Zikmund <karelz@microsoft.com> --------- Co-authored-by: Karel Zikmund <karelz@microsoft.com>
1 parent b355725 commit 24f1513

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,8 @@ public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory<byte> preBuffer,
749749

750750
if (!IsConnectionOriented)
751751
{
752-
var soex = new SocketException((int)SocketError.NotConnected);
753-
return ValueTask.FromException(soex);
752+
var ex = new NotSupportedException(SR.net_notconnected);
753+
return ValueTask.FromException(ex);
754754
}
755755

756756
int packetsCount = 0;

src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ public void SendFile(string? fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpa
12581258
{
12591259
ThrowIfDisposed();
12601260

1261-
if (!Connected)
1261+
if (!IsConnectionOriented || !Connected)
12621262
{
12631263
throw new NotSupportedException(SR.net_notconnected);
12641264
}

src/libraries/System.Net.Sockets/tests/FunctionalTests/SendFile.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public async Task FileDoesNotExist_ThrowsFileNotFoundException(bool useOverloadW
6262
[Theory]
6363
[InlineData(false)]
6464
[InlineData(true)]
65-
[PlatformSpecific(TestPlatforms.Windows)]
6665
public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload)
6766
{
6867
// Create file to send
@@ -77,16 +76,14 @@ public async Task UdpConnection_ThrowsException(bool usePreAndPostbufferOverload
7776

7877
client.Connect(listener.LocalEndPoint);
7978

80-
SocketException ex;
8179
if (usePreAndPostbufferOverload)
8280
{
83-
ex = await Assert.ThrowsAsync<SocketException>(() => SendFileAsync(client, tempFile.Path, Array.Empty<byte>(), Array.Empty<byte>(), TransmitFileOptions.UseDefaultWorkerThread));
81+
await Assert.ThrowsAsync<NotSupportedException>(() => SendFileAsync(client, tempFile.Path, Array.Empty<byte>(), Array.Empty<byte>(), TransmitFileOptions.UseDefaultWorkerThread));
8482
}
8583
else
8684
{
87-
ex = await Assert.ThrowsAsync<SocketException>(() => SendFileAsync(client, tempFile.Path));
85+
await Assert.ThrowsAsync<NotSupportedException>(() => SendFileAsync(client, tempFile.Path));
8886
}
89-
Assert.Equal(SocketError.NotConnected, ex.SocketErrorCode);
9087
}
9188

9289
public static IEnumerable<object[]> SendFile_MemberData()

0 commit comments

Comments
 (0)