解决方案 »

  1.   

    IP或Port若有问题,  就无法连接, 当然要报错。 你有什么想不通的?
      

  2.   


     tcpClient.Connect(IPAddress.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
    NonBlockingprivate bool CheckConnectivityForProxyHost(IPEndPoint remoteEP)
            {            bool isUp = false;
                try
                {
                    client.SendTimeout = 5000;
                    client.ReceiveTimeout = 5000;
                    CallWithTimeout( 50, client, remoteEP);
                    if (client.Connected)
                    {
                        isUp = true;
                    }            }
                catch (Exception)
                {
                    isUp = false;
                }
                finally
                {
                    //try
                    //{
                    //    if (client != null)
                    //    {
                    //        client.Shutdown(SocketShutdown.Both);
                    //    }
                    //}
                    //catch (Exception)
                    //{                //}
                    //finally
                    //{
                    //    if (client != null)
                    //        client.Close();
                    //}            }
                return isUp;
            }
            private void CallWithTimeout( int timeoutMilliseconds, Socket socket, IPEndPoint ipendPoint)
            {
                try
                {
                    Action wrappedAction = () =>
                    {
                        ConnectToProxyServers(socket, ipendPoint);
                    };                IAsyncResult result = wrappedAction.BeginInvoke(null, null);                if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
                    {
                        wrappedAction.EndInvoke(result);
                    }            }
                catch (Exception)
                {            }
            }        private void ConnectToProxyServers(Socket testSocket, IPEndPoint ipEndPoint)
            {
                try
                {
                    if (testSocket == null || ipEndPoint == null)
                        return;                testSocket.Connect(ipEndPoint);            }
                catch (Exception)
                {            }
            } 
      

  3.   

    我的想法是弹窗提示无法连接,而不是程序死掉。
    加个try-catch在你的代码外就行了:         private void button3_Click(object sender, EventArgs e)
              {
                  try
                 {
                  ///your code
                 }
                  catch(Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
              } 
      

  4.   

    我的想法是弹窗提示无法连接,而不是程序死掉。
    加个try-catch在你的代码外就行了:         private void button3_Click(object sender, EventArgs e)
              {
                  try
                 {
                  ///your code
                 }
                  catch(Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
              } 

    非常感谢!
      

  5.   

    程序不会直接死掉,对于没有用到的IP Port会快速的返回结果,不会出现等待20秒以上的情况。