public override void Open() { String[] props = element.Address.Split(':'); if (props.Length != 3) { throw new Exception("Tcp设备通道参数配置不正确"); } int cType = int.Parse(props[0].Trim()); string ipAddress = props[1].Trim(); int port = Int32.Parse(props[2].Trim()); if (cType == 2) { tcpipServer = new TcpListener(IPAddress.Parse(ipAddress), port); tcpipServer.Start(); //创立新线程循环搜索客户端并读取数据 th = new Thread(new ThreadStart(GetData)); th.Start(); } } public void GetData() { //获取当前连接的客户端并读取数据 TcpClient client = null; while (true) { client = tcpipServer.AcceptTcpClient(); ns = client.GetStream(); reciveBytes = null; reciveBytes = new byte[1024]; try { if (ns.CanRead) { do { if (ns.Read(reciveBytes, 0, reciveBytes.Length) > 0) { ActionProcess(reciveBytes, this); } } while (ns.DataAvailable); } } catch (Exception e) { client = tcpipServer.AcceptTcpClient(); ns = client.GetStream(); continue; } } client.Close(); tcpipServer.Stop(); }
reciveBytes接收的数据和客户端发送的数据不一样,求大神指点
客户端是短链接,发送完数据就关闭了
发送的数据可能需要多次接收