throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}
- if (_isConnected)
- {
- throw new SocketException((int)SocketError.IsConnected);
- }
+ ThrowIfConnectedStreamSocket();
ValidateBlockingMode();
throw new ArgumentOutOfRangeException(nameof(port));
}
- if (_isConnected)
- {
- throw new SocketException((int)SocketError.IsConnected);
- }
+ ThrowIfConnectedStreamSocket();
ValidateForMultiConnect(isMultiEndpoint: false); // needs to come before CanTryAddressFamily call
throw new NotSupportedException(SR.net_invalidversion);
}
- if (_isConnected)
- {
- throw new SocketException((int)SocketError.IsConnected);
- }
+ ThrowIfConnectedStreamSocket();
ValidateForMultiConnect(isMultiEndpoint: true); // needs to come before CanTryAddressFamily call
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}
- if (_isConnected)
- {
- throw new SocketException((int)SocketError.IsConnected);
- }
+ ThrowIfConnectedStreamSocket();
// Prepare SocketAddress.
EndPoint? endPointSnapshot = e.RemoteEndPoint;
ObjectDisposedException.ThrowIf(Disposed, this);
}
+ private void ThrowIfConnectedStreamSocket()
+ {
+ if (_isConnected && _socketType == SocketType.Stream)
+ {
+ throw new SocketException((int)SocketError.IsConnected);
+ }
+ }
+
private bool IsConnectionOriented => _socketType == SocketType.Stream;
internal static void SocketListDangerousReleaseRefs(IList? socketList, ref int refsAdded)
Assert.Contains(a.LocalEndPoint.ToString(), ex.Message);
}
}
+
+ [Theory]
+ [MemberData(nameof(LoopbacksAndAny))]
+ public async Task Connect_DatagramSockets_DontThrowConnectedException_OnSecondAttempt(IPAddress listenAt, IPAddress secondConnection)
+ {
+ using Socket listener = new Socket(listenAt.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
+ using Socket s = new Socket(listenAt.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
+ listener.Bind(new IPEndPoint(listenAt, 0));
+
+ await ConnectAsync(s, new IPEndPoint(listenAt, ((IPEndPoint)listener.LocalEndPoint).Port));
+ Assert.True(s.Connected);
+ // According to the OSX man page, it's enough connecting to an invalid address to dissolve the connection. (0 port connection returns error on OSX)
+ await ConnectAsync(s, new IPEndPoint(secondConnection, PlatformDetection.IsOSX ? 1 : 0));
+ Assert.True(s.Connected);
+ }
}
public sealed class ConnectSync : Connect<SocketHelperArraySync>
new object[] { IPAddress.Loopback, true },
new object[] { IPAddress.Loopback, false },
};
+
+ public static readonly object[][] LoopbacksAndAny = new object[][]
+ {
+ new object[] { IPAddress.IPv6Loopback, IPAddress.IPv6Any },
+ new object[] { IPAddress.Loopback, IPAddress.Any },
+ };
}
//