首页 新闻 赞助 找找看

C# TCP 监听端口获取数据(数据量比较大有图片信息)

0
悬赏园豆:20 [已解决问题] 解决于 2014-12-25 15:56
        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();
        }
View Code

reciveBytes接收的数据和客户端发送的数据不一样,求大神指点

问题补充:

客户端是短链接,发送完数据就关闭了

Raylin的主页 Raylin | 菜鸟二级 | 园豆:246
提问于:2014-07-13 11:36
< >
分享
最佳答案
0

发送的数据可能需要多次接收

收获园豆:20
LiuKaiFa | 小虾三级 |园豆:1491 | 2014-07-13 12:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册