Bug 25238 - System.Net.Sockets.TcpClient timeout issue
Summary: System.Net.Sockets.TcpClient timeout issue
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2014-12-10 12:17 UTC by Chris Hamons
Modified: 2014-12-22 12:12 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Chris Hamons 2014-12-10 12:17:15 UTC

We are having difficulties using the System.Net.Sockets.TcpClient class.
We are trying to test if a ip address and port is reachable.

This is a feature from our Windows version.

If we call the Connect method on a valid IpAddress but with an unreachable port it takes a long time (around 1 min) before we get a result.
In the Windows version the same process takes only a few seconds.

To work around the issue we tried adding a time out feature by using the BeginConnect and EndConnect method.
A process that works on the Windows version but in the Mac version I get an ObjectDisposedException.

I have a sample application for you https://www.dropbox.com/s/9g9n7otnou9touw/TestTCPClient.zip?dl=0
Comment 1 Rodrigo Kumpera 2014-12-11 11:26:36 UTC
Hi Marcos,

Could you take a look at this bug?
Comment 2 marcos.henrich 2014-12-19 08:17:23 UTC
Hi Chris, Rodrigo

.NET implementation has indeed a shorter timeout of 20 seconds and Mono a longer one of 85 seconds.

@Chris The method ExecutePortScan(string host, int port, int timeout) has some issues, the exception also occurs on .NET. This happens because tcpClient.EndConnect(result) should never be called after tcpClient.Close (). Please check this fix: https://gist.github.com/esdrubal/4316507e12fefdb7bc63

@Rodrigo Should we change our timeout to 20 seconds as .NET or mark this as a feature?
Comment 3 Miguel de Icaza [MSFT] 2014-12-22 11:03:44 UTC
This sounds to me like a platform specific feature, and we probably should not change that.

In this new era of .NET code, being closer to the underlying platform is a bonus.

This is a clear case of the customer having to alter their code to run on Unix.
Comment 4 marcos.henrich 2014-12-22 12:12:46 UTC
@Miguel I agree.

This timeout can be changed by doing: 
sudo sysctl -w net.inet.tcp.keepinit=20000

Marking this as a feature.