Skip to content

Commit cf66af6

Browse files
committed
fix Connect(SAEA) for connectionless sockets
SO_UPDATE_CONNECT_CONTEXT should not be set, since regular Connect has been called instead of ConnectEx
1 parent 54f8462 commit cf66af6

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,13 @@ private unsafe SocketError FinishOperationConnect()
11681168
{
11691169
try
11701170
{
1171+
if (_currentSocket!.SocketType != SocketType.Stream)
1172+
{
1173+
// With connectionless sockets, regular connect is used instead of ConnectEx,
1174+
// attempting to set SO_UPDATE_CONNECT_CONTEXT will result in an error.
1175+
return SocketError.Success;
1176+
}
1177+
11711178
// Update the socket context.
11721179
SocketError socketError = Interop.Winsock.setsockopt(
11731180
_currentSocket!.SafeHandle,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.IO;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Xunit;
@@ -30,6 +31,18 @@ public async Task Connect_Success(IPAddress listenAt)
3031
}
3132
}
3233

34+
[Theory]
35+
[MemberData(nameof(Loopbacks))]
36+
public async Task Connect_Udp_Success(IPAddress listenAt)
37+
{
38+
using Socket listener = new Socket(listenAt.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
39+
using Socket client = new Socket(listenAt.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
40+
listener.Bind(new IPEndPoint(listenAt, 0));
41+
42+
await ConnectAsync(client, new IPEndPoint(listenAt, ((IPEndPoint)listener.LocalEndPoint).Port));
43+
Assert.True(client.Connected);
44+
}
45+
3346
[OuterLoop]
3447
[Theory]
3548
[MemberData(nameof(Loopbacks))]

0 commit comments

Comments
 (0)