コード例 #1
0
        /// <summary>
        /// 线程池回调方法
        /// </summary>
        /// <param name="state">回调方法要使用的信息对象</param>
        private void ThreadPoolCallback(Object state)
        {   // 如果无法进行转换,则 as 返回 null 而非引发异常
            UdpMessage udpMessage = state as UdpMessage;

            try
            {   // 执行任务
                if (OnThreadTaskRequest != null)
                {
                    OnThreadTaskRequest(udpMessage, EventArgs.Empty);
                }
            }

            catch
            {
                // 阻止异常抛出
            }

            finally
            {   // 关闭连接
                // 从列表中移除连接
                _mutexClients.WaitOne();
                _mutexClients.ReleaseMutex();
            }
        }
コード例 #2
0
        /// <summary>  
        /// 侦听连接线程  
        /// </summary>  
        private void ListenThreadAction()
        {
            // 启动侦听
            //Start();

            // 初始化连接列表和互斥量
            //_tcpClients = new List<TcpClient>();
            _mutexClients = new Mutex();

            IPEndPoint remoteIpEndIPoint = new IPEndPoint(IPAddress.Any, port);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
            // 接受连接
            while (true)
            {
                try
                {
                    if(udpReceive == null)
                        udpReceive = new UdpClient(remoteIpEndIPoint);
                    byte[] receiveBytes = udpReceive.Receive(ref iep);
                    //udpReceive.Close();
                    //udpReceive = null;
                    string question = Encoding.UTF8.GetString(receiveBytes);
                    //MainWindow.insert_file("log.log", "GET:time=" + DateTime.Now.ToString("HH:mm:ss") + ",ques=" + question);
                    UdpMessage udpMessage = new UdpMessage(iep, question);

                    // 接受挂起的连接请求
                    //tcpClient = AcceptTcpClient();

                    // 将该连接通信加入线程池队列
                    ThreadPool.QueueUserWorkItem(ThreadPoolCallback, udpMessage);

                    // 连接加入列表
                    _mutexClients.WaitOne();
                    //_tcpClients.Add(tcpClient);
                    _mutexClients.ReleaseMutex();
                }

                catch (SocketException se)
                {   // 结束侦听线程
                    MainWindow.insert_file("error.log", "0time=" + DateTime.Now.ToString() + "=" + se.Message);
                    //break;
                }

                catch (Exception ee)
                {   // 加入队列失败
                    if (udpReceive != null)
                    {
                        MainWindow.insert_file("error.log", "0time=" + DateTime.Now.ToString() + "=" + ee.Message);
                        udpReceive.Close();
                        udpReceive = null;
                    }
                }
            }
        }