TcpClient.Connect()

  • Thread starter Thread starter Magne Ryholt
  • Start date Start date
M

Magne Ryholt

snippet:
:
private TcpClient tcpReaderClient;
private const int dataPort = n; //of course replaced
IPAddress ipAddress = IPAddress.Parse("nnn.nnn.nnn.nnn"); //of course
replaced
tcpReaderClient = new TcpClient();
tcpReaderClient.Connect(ipAddress, dataPort);
:
Documentation for TcpClient.Connect(IPAddress address, int port) says:
"...The Connect method will block until it either connects or fails..."
since return type is void, I assume a failure means that an exception is
thrown (in general I miss documentation of possible exceptions to be thrown
for methods, or have I missed out something ?),
however in my case an exception is not thrown and the call returns
"immediately", even for non-existing ip addresses or physically
disconnection by unplugging network cable.

I have examined the TcpClient object (also partly the underlying client, an
object of the Socket class), and cannot find anything indicating a failure.
It seems like it works ok if I use one of the non-default constructors for
TcpClient class to set up a connection.

Anyone knows why the connect method is so "quiet" ? (but not the code
executed later of course)

Anyone knows how to control the timeout for tcp connections (not the read or
write timeout, that is well documented) ?
 
Failed connections throw SocketException with ErrorCode set appropriately.

You can set things like timeouts using SetSocketOption.
 
Back
Top