开发者

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

开发者 https://www.devze.com 2023-04-07 14:08 出处:网络
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:

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

精彩评论

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

关注公众号