开发者

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 开发者_JAVA百科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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜