Socket m_clientSocket = null;
            byte[] m_receiveBuffer = new byte[1024];
            m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(server), port);
            try
            {                m_clientSocket.Connect(remoteEndPoint);
                if (m_clientSocket.Connected)
                {
                    fwqzckf();                
                }
                m_clientSocket.Close();
                
            }
            catch (Exception)
            {
                /*MessageBox.Show(e.ToString());*/
                fwqyjgb();
                m_clientSocket = null;
            }
这段代码检测远程服务器  如果远程服务器没有开启  检测时间很长 才能返回错误信息
请问如何能够加快处理呢

解决方案 »

  1.   

    socket类可以设置超时时间: socket   =   new   Socket(hostEP.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp); 
    socket.SetSocketOption(SocketOptionLevel.Socket,   SocketOptionName.SendTimeout,   3000); 
    socket.SetSocketOption(SocketOptionLevel.Socket,   SocketOptionName.ReceiveTimeout,   3000);
      

  2.   


    private void connect(string ipAdd,string port) 

        try 
        { 
            SocketAsyncEventArgs e=new SocketAsyncEventArgs(); 
            m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);         IPAddress ip = IPAddress.Parse(serverIp); 
            int iPortNo = System.Convert.ToInt16(serverPort); 
            IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);         //m_clientSocket. 
            e.RemoteEndPoint = ipEnd; 
            e.UserToken = m_clientSocket; 
            e.Completed+=new EventHandler<SocketAsyncEventArgs>(e_Completed);                 
            m_clientSocket.ConnectAsync(e);         if (timer_connection != null) 
            { 
                timer_connection.Dispose(); 
            } 
            else 
            { 
                timer_connection = new Timer(); 
            } 
            timer_connection.Interval = 2000; 
            timer_connection.Tick+=new EventHandler(timer_connection_Tick); 
            timer_connection.Start(); 
        } 
        catch (SocketException se) 
        { 
            lb_connectStatus.Text = "Connection Failed"; 
            MessageBox.Show(se.Message); 
        } 

    private void e_Completed(object sender,SocketAsyncEventArgs e) 

        lb_connectStatus.Text = "Connection Established"; 
        WaitForServerData(); 

    private void timer_connection_Tick(object sender, EventArgs e) 

        if (!m_clientSocket.Connected) 
        { 
            MessageBox.Show("Connection Timeout"); 
            //m_clientSocket = null;         timer_connection.Stop(); 
        } 
    } connect with timeout for socket.
      

  3.   

    cloudhsu   您的代码缺少
      

  4.   

    有些UI的code...可以自行拿掉...^^
      

  5.   

    “Timer”是“System.Windows.Forms.Timer”和“System.Threading.Timer”之间的不明确的引用
      

  6.   

    呵呵,楼主这个可以用异步的操作来做。可以对操作设置一个等待信号量,如果N秒内未收到操作的返回,可认为操作失败,然后再判断下是否已连接(这样可以避免有时候虽然超时时间已到,但是已经操作成功且未返回或者是临界状态的情况)。
    原理是利用socket默认的超时时间要在16-25秒左右,你可以将等待超时的时间设置在这个值以下,比如1秒或者3秒等。
    示例:   TcpClient tcpClient = new TcpClient();
                    SetMSG("开始连接");                // connflag 连接是否成功的标志
                    bool connflag = false;
                    if (!tcpClient.Connected)
                    {                   
                        try
                        {
                            IAsyncResult result = tcpClient.BeginConnect(ipAddress, _port), null, null);                        connflag = result.AsyncWaitHandle.WaitOne(1000, true);//连接超时1秒如果1S内无返回则认为连接失败,connflag为false,如果有返回则为true。
                        }
                        catch (Exception ex)
                        {
                            SetMSG("连接时出错!错误详细信息:" + ex.Message);
                            connflag = false;
                        }
                    }
                    if (!connflag)
                    {
                        //如果超时无返回
                           if (!tcpClient.Connected)
                            {
                              //肯定是连接失败  
                            }
                            else
                            {
                                //连接成功
                            }
                     }
      

  7.   

    c#的socket本来就可以设置连接的时限的。好像在构造函数那里。设置一个连接的时限不就OK了。
      

  8.   

    public Socket m_clientSocket = null;
            private void connect(string serverIp, string serverPort)
            {
                try
                {
                    
                    SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                    m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPAddress ip = IPAddress.Parse(serverIp);
                    int iPortNo = System.Convert.ToInt16(serverPort);
                    IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);                //m_clientSocket. 
                    e.RemoteEndPoint = ipEnd;
                    e.UserToken = m_clientSocket;
                    m_clientSocket.ConnectAsync(e);
                    if (timer_connection != null)
                    {
                        timer_connection.Dispose();
                    }
                    else
                    {
                        timer_connection = new System.Windows.Forms.Timer();
                    }
                    timer_connection.Interval = 1000;
                    timer_connection.Tick += new EventHandler(timer_connection_Tick);
                    timer_connection.Start();
                }
                catch (SocketException) //se)
                {
                    fwqyjgb();
                    //MessageBox.Show(se.Message);
                }
            }
            private void timer_connection_Tick(object sender, EventArgs e)
            {
                if (!m_clientSocket.Connected)
                {
                    fwqyjgb();
                    //m_clientSocket = null; 
                    timer_connection.Stop();
                }
                else
                {
                    fwqzckf();
                    timer_connection.Stop();
                }
            } 
      

  9.   

    你要的不是只是connect timeout的思路吗?
      

  10.   

    上面的代码或者可以改为下面的,这样更简洁:  TcpClient tcpClient = new TcpClient();
                    SetMSG("开始连接");                // connflag 连接是否成功的标志
                    bool connflag = false;
                    if (!tcpClient.Connected)
                    {                   
                        try
                        {
                            IAsyncResult result = tcpClient.BeginConnect(ipAddress, _port), null, null);                        connflag = result.AsyncWaitHandle.WaitOne(1000, true);//连接超时1秒如果1S内无返回则认为连接失败,connflag为false,如果有返回则为true。
                        }
                        catch (Exception ex)
                        {
                            SetMSG("连接时出错!错误详细信息:" + ex.Message);
                            connflag = false;
                        }
                    }
                    if (!tcpClient.Connected)
                    {
                          //肯定是连接失败  
                    }
                    else
                    {
                          //连接成功
                    }
      

  11.   

    个人认为:winsock的默认超时时间就是十几到二十几秒,如果不用“异步操作-检测信号量”这种讨巧的办法的话,一般情况下不好做到短时间内的连接超时判断,不知道还有没有其他的高手有更好的解决办法。坐等。
      

  12.   

    TcpClient tcpClient = new TcpClient();
                    //SetMSG("开始连接");                // connflag 连接是否成功的标志
                    bool connflag = false;
                    if (!tcpClient.Connected)
                    {                   
                        try
                        {
                            IAsyncResult result = tcpClient.BeginConnect(IPAddress.Parse(server), port, null, null);                        connflag = result.AsyncWaitHandle.WaitOne(500, true);//连接超时1秒如果1S内无返回则认为连接失败,connflag为false,如果有返回则为true。
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("连接时出错!错误详细信息:" + ex.Message);
                            connflag = false;
                        }
                    }
                    if (!tcpClient.Connected)
                    {
                          //肯定是连接失败 
                        fwqyjgb();
                    }
                    else
                    {
                          //连接成功
                        fwqzckf();
                    }