写了个简单的服务器端tcp接收程序,为什么对于每个自动发送信息的客户端,只能收到一次数据,然后就没有然后了。。有没有什么需要注意的地方,求高人指点static void Main(string[] args)
        {
            bool done = false;
            SetServerIPAndPort();
            IPAddress localAddr = IPAddress.Parse(ServerIP);
            TcpListener listener = new TcpListener(localAddr, port);
       listener.Start();
       while (!done)
       {
          Console.Write("Waiting for connection...");
          TcpClient client = listener.AcceptTcpClient();
          Console.WriteLine("Connection accepted.");
                 NetworkStream ns = client.GetStream();
                //BinaryReader br = new BinaryReader(ns);
                //Console.WriteLine(br.ReadString());                 byte[] bytes = new byte[1024];
                 int bytesRead = ns.Read(bytes, 0, bytes.Length);
                 string receiveString = Encoding.ASCII.GetString(bytes, 0, bytesRead);
                 Console.WriteLine(DateTime.Now.ToString() + "  "+ receiveString);
              }
        listener.Stop();
      
        }

解决方案 »

  1.   

    参考下面的形式,AcceptTcpClient这个方法是阻塞的。。
    明白吗? while (this.m_bContinue)
                    {
                        if (this.m_TcpSvr.Pending())
                        {
                            TcpClient handler = this.m_TcpSvr.AcceptTcpClient();
                            if (handler != null)
                            {