开发者

Timeout or try-catch for connecting to a server? (C# WinForms)

Which code is preferable to handling failed connections to a server?

Something like this:

Or this kind of code at the beginning of the async callback for TcpClient.BeginConnect:

try
{
    tcpClient.EndConnect(async);
}
catch
{
    System.Windows.Forms.MessageBox.Show("uh oh");

    return;
}

Tha开发者_运维百科nks for reading


When your context is handling connection timeouts, I assume that you mean you have not yet received a response from a request made, within a certain period of time.

Wrapping your EndConnect in a try block and catching any exceptions thrown does not necessarily constitute a timeout occurance. There is currently no in-built support for handling timeouts with TcpClient. Your first link is a good example of how to detect and work with connection timeouts.

This article may be of some help to you also: Using an asynchronous client socket

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜