额 我不知道怎么描述这个问题啊 就是运行程序的时候form无法显示出来 ,点击没有反应。根据代码运行,执行第一次可以看到界面,但是循环执行的时候就会无法显示出来。注:做的是TCP/IP端口通信的。本人才疏,那个整了3-4天了,只能进行基本的数据发送与接收。这个还是在加了messageBox的情况下,界面才显示出来。去掉messageBox之后便会出现假死的情况。非常郁闷。具体接收部分代码如下:(通过监测本机的8000端口,查看是否有连接)              int portNum = 8000;
            bool done = false;            TcpListener listener = new TcpListener(portNum);            listener.Start();
            TcpClient client = listener.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            while (!done)
            {
                Console.Write("Waiting for connection...");
                ReceiveLB.Items.Add("Waiting for connection...");
                //TcpClient client = listener.AcceptTcpClient();                Console.WriteLine("Connection accepted.");
                ReceiveLB.Items.Add("Connection accepted.");
              
                byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());                byte[] buff=new byte[1000];
                try
                {
                    if (client==null)
                    {
                        client = listener.AcceptTcpClient();
                        ns = client.GetStream();
                    }
//写数据
                    ns.Write(byteTime, 0, byteTime.Length);
//ReceiveLB为listbox,采用线程的话会报线程中无法调用
                    ReceiveLB.Items.Add("send:"+System.Text.Encoding.ASCII.GetString(byteTime));
                    MessageBox.Show(System.Text.Encoding.ASCII.GetString(byteTime));
//读数据
                    ns.Read(buff, 0, buff.Length);
                    ReceiveLB.Items.Add("receive:" + System.Text.Encoding.ASCII.GetString(buff));
                    MessageBox.Show(System.Text.Encoding.ASCII.GetString(buff));
                    //ns.Close();
                   //client.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }            }
           
            client.Close();
            listener.Stop();
-----分割线-----我希望能将接收到得数据保存至数据库,但是现在有个问题是当我长时间不点击messagebox时,会出现若干个重复数据串123456 123456 ......(发送端数据为123456)额 希望有TCP/IP编程经验的高手指点下 有相关C#的源代码请发[email protected]。谢谢

解决方案 »

  1.   

      TcpListener listener = new TcpListener(portNum);  listener.Start();
      TcpClient client = listener.AcceptTcpClient();
      NetworkStream ns = client.GetStream();用一个方法封装......然后用线程启动 (Thread类)
     while (!done) 也循环需要放在一个子线程里
      

  2.   

    可以把MessageBox.Show(System.Text.Encoding.ASCII.GetString(byteTime));
    这个要Show的东西  让他在一个TextBox上显示 方法如下
     delegate void SetTextBoxTextCallBack(string byteTime); //跨线程操作TextBox控件委托        private void UpdateTextBox1Text(string byteTime)
            {
                if (this.TextBox1.InvokeRequired)
                {
                    SetTextBoxTextCallBack m_SetTextBoxTextCallBack = new SetTextBoxTextCallBack(UpdateTextBox1Text);
                    this.BeginInvoke(m_SetTextBoxTextCallBack, new object[] { byteTime});
                }
                else
                {
                    this.TextBox1.Text += byteTime;
                }
            }然后把MessageBox.Show()那里改成UpdateTextBox1Text( byteTime)
      

  3.   

    没必要用messagebox打印日志不就好了。
    这是C#又开源网络组件的,通讯没必要自己写,快速完成业务就好。
      

  4.   

    你要明白一个问题,,,,如果你不创建子线程  默认的使用 UI进程的话
    那你的界面肯定会阻塞就好比 高速公路  同一方向有2条路  是为了超车方便
    如果只有一条的话 你超得过去车? 主次要搞清楚,,你的UI进程是主  不要把他阻塞   
    通过子线程去控制逻辑代码Thread t = new Thread(new ThreadStart(方法名));
    t.Start();这样就可以创建子线程去工作了
      

  5.   

    支持用线程做,以前做到基于UDP协议的文件通讯。
      

  6.   

    额 用线程的话 我绑定到listBox 现在没有出现假死的情况 谢谢
      

  7.   


    //1st
    ManualResetEvent mre=new ManualResetEvent(false);
    int portNum = 8000;
      bool done = false;  TcpListener listener = new TcpListener(portNum);  listener.Start();
      //TcpClient client = listener.AcceptTcpClient();
      NetworkStream ns = client.GetStream();
      while (!done)
      {
    //2nd
      mre.Reset();
      Console.Write("Waiting for connection...");
      ReceiveLB.Items.Add("Waiting for connection...");
    //3rd
      TcpClient client = listener.AcceptTcpClient();
      mre.WaitOne();
      Console.WriteLine("Connection accepted.");
      ReceiveLB.Items.Add("Connection accepted.");
        
      byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());  byte[] buff=new byte[1000];
      try
      {
      if (client==null)
      {
    //???
      client = listener.AcceptTcpClient();
      ns = client.GetStream();
    //4th
      mre.Set();
      }
    //写数据
      ns.Write(byteTime, 0, byteTime.Length);
    //ReceiveLB为listbox,采用线程的话会报线程中无法调用
      ReceiveLB.Items.Add("send:"+System.Text.Encoding.ASCII.GetString(byteTime));
      MessageBox.Show(System.Text.Encoding.ASCII.GetString(byteTime));
    //读数据
      ns.Read(buff, 0, buff.Length);
      ReceiveLB.Items.Add("receive:" + System.Text.Encoding.ASCII.GetString(buff));
      MessageBox.Show(System.Text.Encoding.ASCII.GetString(buff));
      //ns.Close();
      //client.Close();
      }
      catch (Exception e)
      {
      Console.WriteLine(e.ToString());
      }  }
        
      client.Close();
      listener.Stop();
      

  8.   


    //引用集
    using System.Threading;/***********************************/
       private Thread thThreadread;//创建线程,用以侦听端口号,接受信息/*******************/
    ///接收按钮
          private void ReceiveBTN_Click(object sender, EventArgs e)
            {           
                thThreadread = new Thread(new ThreadStart(TcpTimeServer));
                thThreadread.Start();//启动线程
                        }///监听端口,进行连接
    private void TcpTimeServer()
            {
                int portNum = 8000;
                bool done = false;
                TcpListener listener = new TcpListener(portNum);
                listener.Start();
                TcpClient client = listener.AcceptTcpClient();
                NetworkStream ns = client.GetStream();
                while (!done)
                {
                  
                    byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());                byte[] buff=new byte[1000];
                    try
                    {
                        if (client==null)
                        {
                            client = listener.AcceptTcpClient();
                            ns = client.GetStream();
                        }
                        ns.Write(byteTime, 0, byteTime.Length);
                     UpdateListBoxText(System.Text.Encoding.ASCII.GetString(byteTime));
                        ns.Read(buff, 0, buff.Length);
                                                        
       UpdateListBoxText(System.Text.Encoding.ASCII.GetString(buff));
                        
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }            }
                client.Close();
                listener.Stop();
            }
    //可能有错误,我是按照2楼的textbox进行的修改
    //跨线程操作ListBox控件委托
     delegate void SetListBoxCallback(string byteTime);
            private void UpdateListBoxText(string byteTime)
            {
                if (this.ReceiveLB.InvokeRequired)
                {
                    SetListBoxCallback m_SetListBoxCallBack = new SetListBoxCallback(UpdateListBoxText);
                    this.BeginInvoke(m_SetListBoxCallBack, new object[] { byteTime });            }
                else 
                {
                    this.ReceiveLB.Items.Add(byteTime);
                }
            }