当前位置:编程学习 > C#/ASP.NET >>

SOS!一个监听16个客户端的程序总是造成网络连接中断,望不吝解答!

完整代码如下:


private void Startup_Load(object sender, EventArgs e)
 {
TcpInit();
                ReceiveThread = new Thread(new ThreadStart(UdpReceive));
                ReceiveThread.Priority = ThreadPriority.BelowNormal;
                ReceiveThread.Start();
}

 private int TcpInit() 
        {
            //tcp初始化      
            tcpListen = new TcpListener(12345);            

            tcpListen.Start();

            tcpClient = new TcpClient[16];
            ns = new NetworkStream[16];

            for (int i = 0; i < 16; i++)
            {
                serialUdpRecvCoun[i] = 0;
            }
}

private void acceptTcpListener()
        {
            for (int i = 0; i < 16; i++)
            {
                if (tcpClient[i] == null)
                {
                    tcpClient[i] = tcpListen.AcceptTcpClient();
                    tcpClient[i].ReceiveBufferSize = 1024;
                    ns[i] = tcpClient[i].GetStream();
                    return;
                }
            }
        }

 private void UdpReceive()
        {
            while (true)
            {
                try
                {
                    if (tcpListen.Pending())
                    {
                        Thread tempAccept = new Thread(new ThreadStart(acceptTcpListener));
                        tempAccept.Priority = ThreadPriority.BelowNormal;
                        tempAccept.Start();
                        //if (TcpCount)
                        //TcpCount++;                    
                    }


                    for (int k = 0; k < 16; k++)
                    {
                        if (tcpClient[k] != null && tcpClient[k].Connected == false)
                        {
                            //tcpClient = tcpListen.AcceptTcpClient();
                            //tcpClient.ReceiveBufferSize = 1024;                       
                            tcpClient[k] = null;
                        }

                        if (tcpClient[k] != null && tcpClient[k].Connected == true)
                        {
                            bool find = false;

                            //NetworkStream nsData = tcpClient[k].GetStream();

                            byte[] buf = new byte[1024];

                            try
                            {
                                if (ns[k].DataAvailable == true)
                                {
                                    count = ns[k].Read(buf, 0, 512);
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            catch
                            {
                                continue;
                            }
             }
} --------------------编程问答-------------------- 现在测试连接了6台服务端,但是4小时或者更多后只剩下一台连接,另外5台网络断掉了 --------------------编程问答-------------------- 代码肯定有错,望指出
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,