Client Listening w/out Exceptions ?

NeuralJack

Well-known member
Joined
Jul 28, 2005
Messages
138
So, sure the TcpListener will sit there and listen.. but for an app of mine id like the Client to sit there and wait for a Host to accept the connection.

The way Im doing it is putting the TcpClient.connect through a Loop. I just dont like how each time the TcpClient attempts to connect and fails it throws an Exception.

Is there another way to make the TcpClient just sit there, quietly, waiting for a host to accept the connection w/out throwing socket exceptions?

So far i have tried TcpClient.connect, TcpClient.BeginConnect with AsyncWaitHandle.WaitOne(), etc. No luck... probably because the connection method actually exits on a connection failure each time.
 
Listening vs connecting

If a connection attempt fails, it is logical for a socket to throw an exception. Comparing a listening socket with a connecting socket is misleading - listening for connections is a passive operation, whereas connecting is an active operation with (essentially) immediate results.

There are a number of reasons a connection attempt could fail, and you should check this (using SocketException.ErrorCode for example), before deciding whether to attempt another connection, or to perform some other action. Conversely, a listening socket doesnt even throw SocketExceptions once it has started listening - it is waiting for something to happen.

If you feel that you really must have this behaviour, then implementing a loop as you have done, with a short wait between attempts, is probably the easiest solution.

Good luck :)
 
The error is that the machine actively refused it because the host/listener is not listening. The error doesnt happen if the host is online of course.

I did want this behavior as an option so that a client could load up on a computer after windows startup and never have to be touched. It will essentially wait , if it has to, for the host/listener to start on another computer.

I will continue to use the loop. Thanks Paul.
 
Back
Top