Ryan Peschel
Ryan Peschel

Reputation: 11996

How to set the the timeout for an asychronous Connection with a TcpClient?

I've been looking at numerous examples such as this, this, and this but none of them seem to work. I'm not sure what's wrong but for each example I've tried the wait is just ignored and I just get the standard unhandled socket exception in approximately 1 second, regardless of the specified wait time. No connection could be made because the target machine actively refused it.

Here's an excerpt to help understanding:

public void Connect(string host, int port)
{
    tcpClient.BeginConnect(host, port, OnConnect, null);
}

private void OnConnect(IAsyncResult async)
{
    tcpClient.EndConnect(async);
}

Been trying a bunch of different examples but for all of them my client application either just failsfast after a second or throws a socket exception. Would a try-catch be a better solution here and just avoid using WaitHandles?

Upvotes: 0

Views: 311

Answers (1)

dumdum
dumdum

Reputation: 868

the error

No connection could be made because the target machine actively refused it.

usually means that there is nothing running on that port on the remote machine, or there is a firewall somewhere blocking the request.

there is a

tcpClient.SendTimeout

and tcpClient.ReceiveTimeout to answer your specific question, but it probably isn't your issue here.

Upvotes: 2

Related Questions