How to check if a server is listening without exception handling

I am working on two applications that connect to each other using TCP. At some point, one of them tries to connect using TcpClient, but the other application does not guarantee that it is already starting to listen (using TcpListener).

My first attempt:

TcpClient c = null; while (true) { try { c = new TcpClient(); c.NoDelay = true; c.Connect( ip, port ); break; } catch (SocketException ex) { Console.WriteLine(string.Format("Could not connect to {0}:{1}, retrying...", ip, port)); Thread.Sleep( 500 ); } } 

However, the problem is that it relies on exception handling, which is a bit unpleasant for me, because I have VS to catch any thrown exceptions (menu "Debug → Exceptions ..."). Therefore, every time it tries to connect, it splits into VS, and I need to click continue.

An exception I get:

No connection can be made because the target computer actively rejected it 127.0.0.1►0000

I would suggest that it will be possible to check if the server is listening on any port without trying to connect to it. I understand that just checking will not be enough - the server may go down between checking and trying to connect, but I still will be much better while I develop it.

Or something like this:

 TcpClient tcpClient = new TcpClient(); while ( !tcpClient.TryConnect(....) ) { Thread.Sleep(1000); } 

Similar to this:

 if (bool.TryParse( "false" )) { ... } 

I also tried using async methods (Begin / End Connect) and manually setting a timeout for ManualResetEvent, but that didn't work either. I browsed the Internet, but I could not find a solution to this problem, so I finally post here :)

+4
source share
2 answers

Is the problem that VS is breaking Exception? You can always ignore the VS individual family of Exceptions.

In VS, from the Debug menu, select "Exceptions ...", and in the dialog box presented, you can control this.

+4
source

I was going to propose not to catch the exception, so before I suggested checking it myself, and if you set it to exclude all exceptions, even if you did not select an exception, all its exceptions will still be thrown. I have to agree with Shiv Kumar, either adjust my settings while debugging your application, or accept the limitations of what you do.

The reason bool.TryParse works is to check every character, just as Int32.TryParse ensures that every character in a string is 0-9 or any other valid numeric character.

Of course, you could write your own network class and not throw an exception if the connection failed.

TryParse will not throw an exception, you must catch any exception that is thrown if you use bool.Parse try {} catch {} otherwise, if you try to parse something that is not a boolean, it will throw an unhandled exception. TryParse was added later in .NET history, Parse is a more classic method that allows the programmer to process all unexpected data and check input before trying to parse the data.

I must add that TryParse will return false if it could not parse the value in both results of the method, is false, and the out variable, which I consider to be true, is false. This, at least, takes place with Int32

http://msdn.microsoft.com/en-us/library/system.boolean.tryparse.aspx

I assume the point of indicating how TryParse and Parse work is that they are completely different animals compared to TcpClient. I suggest that I should clarify that the basic validation process is similar, except that one throws an exception and the other does not, and, of course, one returns what was actually analyzed.

+1
source

All Articles