使用TcpListener监听8000端口,希望读取该端口的数据并保存,客户端是一直往该端口发送着数据的,但发现只是开始的时候读到几k的数据,后面就没有了,代码如下,大家看看,在控制台程序中可以正常接收,我想把他改成一个函数,然后窗体Load时使用线程打开该函数,进行监听,代码如下:大家帮看看。
  private void ListenReal()
        {
            TcpListener tcpLiestner = new TcpListener(IPAddress.Parse("192.168.1.190"), 8000);
            tcpLiestner.Start();            TcpClient client = null;            Byte[] bytes = new Byte[1024];
            String data = null;            while (true)
            {
                
                try
                {  
                        client = tcpLiestner.AcceptTcpClient();
                        Console.WriteLine("Connected!");                        data = null;
                        NetworkStream stream = client.GetStream();                        int i;  
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {  
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            System.IO.File.AppendAllText(@"D:\123.dat", data, Encoding.ASCII);
                        }                
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
            }
            client.Close();
            tcpLiestner.Stop();
            
        }
控制台程序如下(控制台程序能实现很好的监听):
 public static void Main()
        {
            TcpListener server = null;
            try
            {
                Int32 port = 8000;
                IPAddress localAddr = IPAddress.Parse("192.168.1.190");
                server = new TcpListener(localAddr, port);
                server.Start();                Byte[] bytes = new Byte[1024];
                String data = null;                while (true)
                {
                    Console.Write("Waiting for a connection... ");
 
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");                    data = null;
                    NetworkStream stream = client.GetStream();                    int i;
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);
                        System.IO.File.AppendAllText(@"D:\123.dat", data, Encoding.ASCII);
                    }
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                server.Stop();
            }
            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }  
那么这个控制台的程序怎么改成一个循环监听的函数呢,其中的错误在哪里呢?
请路过的指点下。

解决方案 »

  1.   

    TNND,自己用socket读取的方式解决问题!
      

  2.   


    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            Console.WriteLine("Received: {0}", data);
                            System.IO.File.AppendAllText(@"D:\123.dat", data, Encoding.ASCII);
                        }
    不出问题才怪~~