문제점

 아래처럼 소켓의 Connect 메소드를 사용할 경우 서버가 접속불가능한 상태에서는 Timeout 시간이 너무 길다. 

m_clientSocket.Connect(ipEnd);


해결방법

public static class SocketExtensions
{
    /// <summary>
    /// Connects the specified socket.
    /// </summary>
    /// <param name="socket">The socket.</param>
    /// <param name="endpoint">The IP endpoint.</param>
    /// <param name="timeout">The timeout.</param>
    public static void Connect(this Socket socket, EndPoint endpoint, TimeSpan timeout)
    {
        var result = socket.BeginConnect(endpoint, null, null);

        bool success = result.AsyncWaitHandle.WaitOne(timeout, true);
        if (success)
        {
            socket.EndConnect(result);
        }
        else
        {
            socket.Close();
            throw new SocketException(10060); // Connection timed out.
        }
    }
}


참조

http://stackoverflow.com/questions/1062035/how-to-configure-socket-connect-timeout



+ Recent posts