/// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainStationForm_Load(object sender, EventArgs e)
        {
            IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.189"), 12717);            //启动一个线程来接受
            this.myThread = new Thread(new ThreadStart(
                delegate()
                {
                    //监听
                    TcpListener tempTcpListener = new TcpListener(iPEndPoint);                    //开启监听
                    tempTcpListener.Start(20);                    while (true)
                    {
                        //获得tcpClient
                        this.tcpClient = tempTcpListener.AcceptTcpClient();                        //实例化流
                        this.receiveNetworkStream = this.tcpClient.GetStream();                        //设置接受缓存区大小
                        this.receiveBytes = new byte[this.tcpClient.Available];                        if (receiveBytes.Length != 0)
                        {
                            //异步方法读取数据
                            this.receiveNetworkStream.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), this.receiveNetworkStream);
                        }
                    }
                }
                ));
        this.myThread.IsBackground = true;
            this.myThread.Start();
        }        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void RecvCallback(IAsyncResult ar)
        {            NetworkStream ns = (NetworkStream)ar.AsyncState;            //调用数据流的读取方法
            ns.EndRead(ar);
        }为什么 while (true) 里面的东西只执行一次?就执行我连接的那一次...客户端再发消息过来就不再执行和理会了......
                    

解决方案 »

  1.   

    应该是抛出异常从while(true)中退出了,你在while(true)外边用try/catch抓异常看看,是什么异常,估计是你没关闭那些打开的流,
      

  2.   

     //启动一个线程来接受
                this.myThread = new Thread(new ThreadStart(
                    delegate()
                    {
                        //监听
                        TcpListener tempTcpListener = new TcpListener(iPEndPoint);                    //开启监听
                        tempTcpListener.Start(20);
                        try     
                     {
                        while (true)
                        {
                            
                                //获得tcpClient
                                this.tcpClient = tempTcpListener.AcceptTcpClient();                            //实例化流
                                this.receiveNetworkStream = this.tcpClient.GetStream();                            //设置接受缓存区大小
                                this.receiveBytes = new byte[this.tcpClient.Available];                            if (receiveBytes.Length != 0)
                                {
                                    //异步方法读取数据
                                    this.receiveNetworkStream.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), this.receiveNetworkStream);
                                }
                            }
                       }
                            catch (Exception ex)
                            {
                                                         
                            }                  
                    }
                    ));
    修改后的代码...也没有抓到异常......
      

  3.   

    你while里只有this.tcpClient = tempTcpListener.AcceptTcpClient();这个不是指接收信息,而接收客户的连接请求,你要循环接收一个已连接上的客户信息应该在回调函数中再次加入接收信息的调用 /// <summary>
      /// 回调函数
      /// </summary>
      /// <param name="ar"></param>
      private void RecvCallback(IAsyncResult ar)
      {  NetworkStream ns = (NetworkStream)ar.AsyncState;  //调用数据流的读取方法
      ns.EndRead(ar);
      //处理接收到的消息   //再次调用异步接收  ns.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), this.receiveNetworkStream);  }
      

  4.   

    上面写错了一点/// <summary>
      /// 回调函数
      /// </summary>
      /// <param name="ar"></param>
      private void RecvCallback(IAsyncResult ar)
      {  NetworkStream ns = (NetworkStream)ar.AsyncState;  //调用数据流的读取方法
      ns.EndRead(ar);
      //处理接收到的消息   //再次调用异步接收  ns.BeginRead(receiveBytes, 0, receiveBytes.Length, new AsyncCallback(RecvCallback), ns);  }