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
精彩评论