这是我的方法 但这个方法在检测比较多的数据的时候非常卡 foreach (DataGridViewRow row in this.Dgvdete.Rows)
      
     {
              string ips= row.Cells[0].Value.ToString();
              string port= row.Cells[1].Value.ToString();
              IPAddress ip = IPAddress.Parse(ips);
              DataGridViewImageCell imgcell = (DataGridViewImageCell)row.Cells[2];
              imgcell.Value = imageList1.Images[1];
              try
              {
                      IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(port));
                      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                      sock.SendTimeout = 1;
                      sock.Connect(point);
                      imgcell.Value = imageList1.Images[0];
                  //Execute(string dosCommand, int milliseconds)
              }
              catch
              {
                  imgcell.Value = imageList1.Images[1];
              }
            }希望哪位同行能告诉我比较快捷的检测方法!

解决方案 »

  1.   

    使用异步去连接BeginConnect或者直接去Ping
      

  2.   

    Ping我也用过可以设置过期时间!但我门经理不用ping异步是怎么连接啊!能给我一段代码吗?谢谢!!!!!!
      

  3.   


    Socket s = new Socket(lep.Address.AddressFamily,
                                       SocketType.Stream,
                                             ProtocolType.Tcp);s.BeginConnect(lep, new AsyncCallback(ConnectCallback), s);
    private static void ConnectCallback(IAsyncResult ar) {
            try {
                // Retrieve the socket from the state object.
                Socket client = (Socket) ar.AsyncState;            // Complete the connection.
                client.EndConnect(ar);            Console.WriteLine("Socket connected to {0}",
                    client.RemoteEndPoint.ToString());            // Signal that the connection has been made.
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }
        }以上摘自MSDN
      

  4.   

    对了,可以 BeginConnect 也可以 AsynchronousSocket
      

  5.   

    兄弟因为我对Socket 这一块不熟所以不知道那个lep是怎么实例化的!!!!!
      

  6.   

    因为同步的连接无法设置连接超时时间,等待连接的过程中界面会死掉。 IPHostEntry lipa = Dns.Resolve("host.contoso.com");
    IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);       Socket s = new Socket(lep.Address.AddressFamily,
                                       SocketType.Stream,
                                             ProtocolType.Tcp);
           try{                 while(true){
                     allDone.Reset();                 Console.WriteLine("Establishing Connection");
                     s.BeginConnect(lep, new AsyncCallback(Async_Send_Receive.Connect_Callback), s);                 allDone.WaitOne();
                }
           }
           catch (Exception e){
                Console.WriteLine(e.ToString());
           }
    哈哈
      

  7.   

    连接不上的站大多数吗?如果是的话,那可不能把连接失败的处理代码方在catch{}中!那样会很慢的!