By RazanPaul
Translator: Todd Wei
Original article: http://www.codeproject.com/KB/IP/TimeOutSocket.aspx
Introduction
You may have noticed that both System. Net. Sockets. TcpClient and System. Net. Sockets. Socket of. Net do not directly provide timeout control mechanisms for Connect/BeginConnect. Therefore, when the server is not listening, or a network failure occurs, the client connection request will be forced to wait for a long time until an exception is thrown. The default wait time is 20 ~ 30 s .. SocketOptionName. SendTimeout of the Net Socket Library provides control over the timeout time for data sending, but it is not the timeout time for connection requests discussed in this article.
Background
This problem was originally caused by a project in which I posted key code on my blog. I have noticed many thanks for this, so I think this is a common problem. Maybe many people need to solve it.
Implementation
The following is the key code for implementation:
Class TimeOutSocket
{
Private static bool IsConnectionSuccessful = false;
Private static Exception socketexception;
Private static ManualResetEvent TimeoutObject = new ManualResetEvent (false );
Public static TcpClient Connect (IPEndPoint remoteEndPoint, int timeoutMSec)
{
TimeoutObject. Reset ();
Socketexception = null;
String serverip = Convert. ToString (remoteEndPoint. Address );
Int serverport = remoteEndPoint. Port;
TcpClient tcpclient = new TcpClient ();
Tcpclient. BeginConnect (serverip, serverport,
New AsyncCallback (CallBackMethod), tcpclient );
If (TimeoutObject. WaitOne (timeoutMSec, false ))
{
If (IsConnectionSuccessful)
{
Return tcpclient;
}
Else
{
Throw socketexception;
}
}
Else
{
Tcpclient. Close ();
Throw new TimeoutException ("TimeOut Exception ");
}
}
Private static void CallBackMethod (IAsyncResult asyncresult)
{
Try
{
IsConnectionSuccessful = false;
TcpClient tcpclient = asyncresult. AsyncState as TcpClient;
If (tcpclient. Client! = Null)
{
Tcpclient. EndConnect (asyncresult );
IsConnectionSuccessful = true;
}
}
Catch (Exception ex)
{
IsConnectionSuccessful = false;
Socketexception = ex;
}
Finally
{
TimeoutObject. Set ();
}
}
}
Here, WaitOne (TimeSpan, Boolean) of ManualResetEvent plays a major role. It blocks the current thread until the ManualResetEvent object is Set or exceeds the timeout time. In the above Code, after BeginConnect is called, the current thread is blocked by the WaitOne method. If the connection is successful within the timeoutMSec time, TimeoutObject will be called in the CallBackMethod callback. set to remove the blocked connection thread and return it; otherwise, the connection thread will automatically close the connection and throw TimeoutException after waiting for timeout.
Summary
Although the implementation is very simple, many people may need to connect to the request timeout mechanism. If you have any questions, I will try my best to answer them for you.
[Comment]
The author introduces a connection request timeout mechanism of asynchronous connection + WaitOne. The implementation details are debatable, for example, thread security issues caused by a. static member; B. You can consider using IAsyncResult. AsyncWaitHandle without having to create ManualResetEvent. Thanks for the solution.