Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Fix ConnectAsync not throwing exception for already connected Socket #27173

Merged
merged 1 commit into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,11 @@ public void Connect(EndPoint remoteEP)
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

ValidateBlockingMode();

if (NetEventSource.IsEnabled)
Expand Down Expand Up @@ -842,6 +847,11 @@ public void Connect(IPAddress address, int port)
throw new ArgumentOutOfRangeException(nameof(port));
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

ValidateForMultiConnect(isMultiEndpoint: false); // needs to come before CanTryAddressFamily call

if (!CanTryAddressFamily(address.AddressFamily))
Expand Down Expand Up @@ -917,6 +927,11 @@ public void Connect(IPAddress[] addresses, int port)
throw new NotSupportedException(SR.net_invalidversion);
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

ValidateForMultiConnect(isMultiEndpoint: true); // needs to come before CanTryAddressFamily call

ExceptionDispatchInfo lastex = null;
Expand Down Expand Up @@ -2074,6 +2089,11 @@ public IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback callback, obje
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}


DnsEndPoint dnsEP = remoteEP as DnsEndPoint;
if (dnsEP != null)
Expand Down Expand Up @@ -2154,6 +2174,11 @@ public IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCal
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

IPAddress parsedAddress;
if (IPAddress.TryParse(host, out parsedAddress))
{
Expand Down Expand Up @@ -2201,6 +2226,11 @@ public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requ
throw new ArgumentOutOfRangeException(nameof(port));
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

ValidateForMultiConnect(isMultiEndpoint: false); // needs to be called before CanTryAddressFamily

if (!CanTryAddressFamily(address.AddressFamily))
Expand Down Expand Up @@ -2243,6 +2273,11 @@ public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

ValidateForMultiConnect(isMultiEndpoint: true);

// Set up the result to capture the context. No need for a lock.
Expand Down Expand Up @@ -3804,6 +3839,11 @@ public bool ConnectAsync(SocketAsyncEventArgs e)
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}

if (_isConnected)
{
throw new SocketException((int)SocketError.IsConnected);
}

// Prepare SocketAddress.
EndPoint endPointSnapshot = e.RemoteEndPoint;
DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;
Expand Down
2 changes: 0 additions & 2 deletions src/System.Net.Sockets/tests/FunctionalTests/Connect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public async Task Connect_MultipleIPAddresses_Success(IPAddress listenAt)
}
}

[OuterLoop] // TODO: Issue #11345
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO - put this back in

[Fact]
[ActiveIssue(22765, TestPlatforms.AnyUnix)]
public async Task Connect_OnConnectedSocket_Fails()
{
int port;
Expand Down