用tcpClient连接,一直不出错,网线拔了也不出错,write也没不出错,read时抛了一个异常。
        try
        {
            TcpClient m_tcpClient = new TcpClient();    //是否以创建对象    //IPAddress myIp =             if (!m_tcpClient.Connected)//是否以连接
            {
                int AddrPort = 80;//目标端口
                IPAddress address = IPAddress.Parse("192.168.1.50");//目标ip
                m_tcpClient.Connect(address, AddrPort);           
             }            m_tcpClient.SendTimeout = 10000;
            m_tcpClient.ReceiveTimeout = 10000;            byte[] buffer = new Byte[256];            if (m_tcpClient.Connected)
            {
                NetworkStream ns = m_tcpClient.GetStream();                byte[] bytes = new byte[SendText.Length];//建立缓冲区
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = Convert.ToByte(SendText[i]);
                }                Byte[] result = new byte[10];                // 发送数据到远程UPS控制器
                ns.Write(bytes, 0, bytes.Length);                ns.Read(result, 0, result.Length);                ns.Close();
                m_tcpClient.Close();                return (System.Text.Encoding.ASCII.GetString(result));
            }
            else
                throw new Exception("未连接");        }
        catch (System.Exception e)
        {
            string strError = "发送失败:" + e.Message;
            return strError;
        }

解决方案 »

  1.   

    connect也没有返回值,也不抛异常,不明白呀
      

  2.   

    网上查都是这样,MSDN说一直连接到失败或成功,难道网线拔了也回成功么
      

  3.   

    网络有延时,如果你已经connect了,这时你拔掉网线,不是一下就能判断出来的,需要等待一下下。
    如果是先拔掉网线,你先用ping命令看看能不能连接,不能的话,你再运行程序试试。
      

  4.   

    还是这个问题,没异常,connect和write都没异常,网线都是断掉的,缓存也清了,还是不行,
    肯定是没连上。