在本机的2000端口监听客户端连接,然后在页面上显示传送的文件内容;
问题如下:第一次传送是可以显示的;但是不能连续显示第二次传入的内容?
请教高手指点一下,谢谢!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;namespace TcpReceiver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Thread thread = new Thread(new ThreadStart(Listen));
            thread.Start();
        }        protected void Listen()
        {
            try
            {
                // IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
                IPAddress ipAddress = IPAddress.Any;
                Int32 port = 2000;
                TcpListener tcpListener = new TcpListener(ipAddress, port);
                tcpListener.Start();                //TcpClient tcpClient = tcpListener.AcceptTcpClient();  这里是同步调用会阻塞后面的请求
                tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);               //这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了: \n" + ex.Message, "错误");
            }
          
        }        protected void AcceptClient(IAsyncResult ar)
        {
            try
            {
                TcpListener tcpListener = (TcpListener)ar.AsyncState;
                TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
                NetworkStream networkStream = tcpClient.GetStream();
                StreamReader streamReader = new StreamReader(networkStream);
                string content = streamReader.ReadToEnd();                rtbxContent.Invoke(new MethodInvoker(delegate
                {
                    rtbxContent.Text = "";
                    rtbxContent.Text = content;
                }));                networkStream.Close();
                tcpClient.Close();
                //tcpListener.Stop();
            }
            catch (Exception ex) 
            {
                MessageBox.Show("出错了: \n" + ex.Message, "错误");
            }
            
        }
    }
}

解决方案 »

  1.   

    你先new一个Thread,在里面:while()
    {
       Socket socket = tcplistener.AcceptSocket();
       if (socket != null)
            StartItem(socket);}
      

  2.   

    你这个Listen和AcceptClient的方法也就被执行了一次吧,里面也没有while循环取读取数据,读了一次,就结束线程了
      

  3.   

    tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
    ------------
    上面这句在protected void AcceptClient(IAsyncResult ar)函数的尾部再加上一句,不过你这样加上效果也不会很好,不如while来的实在
      

  4.   

    用while如何写呢?是不是要用到TcpClient[] tcpClients的数组,将所监听到得tcpClient都加入到数组中,然后再操作;
    不是很明白;是要对所有的tcpclient执行操作还是要怎么样操作;
    我目前的测试程序就是显示最后一个连接发送的数据;
    我理解,监听一直开着,有连接请求就会执行protected void AcceptClient(IAsyncResult ar)方法;
    谢谢!
      

  5.   

    不用TcpClient[] tcpClients
    你用完了就关闭就行了
      

  6.   

    我理解:监听是一直开着的;有连接请求、然后就要执行protected void AcceptClient(IAsyncResult ar);
    我的理解可能是错误的!(90%)
    为什么还要while呢?如果用while,那么用while是循环什么对象?该怎么写 ,谢谢!
      

  7.   

    protected void AcceptClient(IAsyncResult ar)
    {
    ......................
    while()
    {
      TcpListener tcpListener = (TcpListener)ar.AsyncState;
      TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
      NetworkStream networkStream = tcpClient.GetStream();
      StreamReader streamReader = new StreamReader(networkStream);
      string content = streamReader.ReadToEnd();  rtbxContent.Invoke(new MethodInvoker(delegate
      {
      rtbxContent.Text = "";
      rtbxContent.Text = content;
      }));  networkStream.Close();
      tcpClient.Close();
    }
    }
    是这样吗?
      

  8.   

    不是,晕,你先new一个线程,然后再里面while,你还是看下百度上面的例子吧,搜C#网页代理程序,研究完这个例子,你就差不多了
      

  9.   

    namespace TcpReceiver
    {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      Thread thread = new Thread(new ThreadStart(Listen));
      thread.Start();

      }  protected void Listen()
      {
      try
      {
      // IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
      IPAddress ipAddress = IPAddress.Any;
      Int32 port = 2000;
      TcpListener tcpListener = new TcpListener(ipAddress, port);
      tcpListener.Start();  //TcpClient tcpClient = tcpListener.AcceptTcpClient(); 这里是同步调用会阻塞后面的请求
      tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);  //这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
      }
      catch (Exception ex)
      {
      MessageBox.Show("出错了: \n" + ex.Message, "错误");
      }
        
      }
      

  10.   

    应该是加在这里吧,明天上班试试!
    while(true)
    {
       tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
    }
      

  11.   

     TcpListener tcpListener = (TcpListener)ar.AsyncState;
                TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
                while (true)
                {。。
    ----求分
      

  12.   

     //networkStream.Close();
                        //tcpClient.Close();
                        //tcpListener1.Stop();这个不能关闭 要去掉
      

  13.   

    while(true)
    {
      tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
    }这样加了以后,功能倒是可以了;
    但是性能大打折扣,接收端不久就outofmemoryException了!
      

  14.   

    我这里是new 了一个thread,然后在thread的方法里面while的:
     public Form1()
            {
                InitializeComponent();
                Thread thread = new Thread(new ThreadStart(Listen));
                thread.Start();
            }
            TcpListener tcpListener = null;
            protected void Listen()
            {
                try
                {
                    // IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
                    IPAddress ipAddress = IPAddress.Any;
                    Int32 port = 2000;
                    tcpListener = new TcpListener(ipAddress, port);
                    tcpListener.Start();                //TcpClient tcpClient = tcpListener.AcceptTcpClient();  这里是同步调用会阻塞后面的请求
                    while (true)
                    {
                        tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), tcpListener);
                    }
                    
                   //这里采用异步调用,接收一个连接请求,就开启一个线程来处理请求
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出错了: \n" + ex.Message, "错误");
                }
            }