private void service()
        {
            //Buffer for reading data
            Byte[] bytes = new Byte[10000];
            String data = null;            data = null;            //Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();
            int i;
           ///////int i = stream.Read(bytes, 0, bytes.Length);            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                //Translate data bytes to a ASCII string 
                data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
                this.listBox1.Items.Add("接受数据:" + data);                data = data.ToUpper();
                byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);                //Send a response
                stream.Write(msg, 0, msg.Length);                this.listBox1.Items.Add("发送数据:" + data);
            }
            client.Close();
        }上面这个方法我用来处理来自客户端的数据,但当接受了从GPRS和PDA上的数据后,处理的方法不一样,GPRS的要走while循环 可PDA的不用走循环为什么?

解决方案 »

  1.   

    你上面的功能是什么,模拟请求?按说接收数据,你只要判断当前接收的数据是否小于buffer的长度,那么就说明已经接收完毕了。
      

  2.   

    上面是用现成调用这个方法
     private void listen()
            {
                try
                {
                    //Set the TcpListener on the port
                    Int32 port = 8081;
                    IPAddress ipAddress = IPAddress.Parse("192.168.1.222");                tcpListener = new TcpListener(ipAddress, port);
                    //Start Listening for client requests
                    tcpListener.Start();                this.listBox1.Items.Add("服务器已经启动.....");
                  
                    while (true)
                    {                    //Get a socket object to accept requests
                        client = tcpListener.AcceptTcpClient();
                        ///////// clientSend = client;
                        this.listBox1.Items.Add("有客户连接上!");
                        thread1 = new Thread(new ThreadStart(service));
                        thread1.Start();
                        
                    }            }
                catch (SocketException e)
                {
                    MessageBox.Show("SocketException :" + e.ToString());
                }
                finally
                {
                    tcpListener.Stop();
                }          
            }
      

  3.   

    to 请问C#里面有没有提供判断传送数据的客户段的类型的方法或类,比如是PDA还是GPRS都走的是tcp/ip协议,无法判断。两者发送数据有什么不同吗