接收信息需要在循环里进行,而receive本身是阻塞方法,所以你不用担心cpu占漫,完全可以使用while(true)或者加一个退出条件
while(true) {
try
     {
// **STEP 2**   接收信息
byte[] receiveBytes=receive.Receive(ref RemoteIpEndPoint);    
//信息转换并输出
string returnData=Encoding.ASCII.GetString(receiveBytes);
this.richTextBox1.AppendText(returnData);
// **STEP 3**关闭连接
receive.Close();
     }
    catch(Exception e)
         {
      MessageBox.Show(e.ToString());
          }
}

解决方案 »

  1.   

    兄弟udpclient 的 receive方法本身是阻塞方法,所以你写在死循环里是没有问题的,如果你想有个退出条件你可以写到while的条件里另外你这种接收网络消息的东西肯定是需要开一个新的线程的,你要写在主线程里肯定是行不通的,因为receive要阻塞当前线程,所以你写的那代码也就是接收到了一个消息然后主线程才开始向前运行的,所以你的界面开始出不来,这也是你的第二个问题,同样是由于你receive没有用正确。唉,好心给你解释,被你认为是捣乱... 郁闷
      

  2.   

    蒙了。。
    xxdneu(xxd)  联系一下啊,
    问点问题,好不好?
      

  3.   

    看一看quickstart里的udp chat例子吧,我越解释越你越蒙
      

  4.   

    用多线程控制,创建一线程处理接收数据的方法。该方法弄个死循环,这样才可以不断接收数据你是不是点一下udpServer发一条,然后再接收一条?所以你的方法是不对的,不能每次都new 一个udpClient,这样会造成在发送时,这个udpClent不存在,
    当然它生成时,就不可能接收到了.
    你可以把它设为全局的,
      

  5.   

    private void Form1_Load(object sender, System.EventArgs e)
    {
    Thread start = new Thread(new ThreadStart(Receive));
    start.Start();


    }
    private void Receive()
    {   
    // **STEP 1**
    UdpClient receive= new UdpClient(8080);
    IPEndPoint RemoteIpEndPoint=null;
        try
    {
    while(true)
      {
      // **STEP 2**   receiving the data
      byte[] receiveBytes=receive.Receive(ref RemoteIpEndPoint);    
      //Conver the data 
      string returnData=Encoding.ASCII.GetString(receiveBytes);
     this.richTextBox1.Text= (DateTime.Now+"\r\n"+returnData);
      // **STEP 2**      
      receive.Close();
      }


    }
    catch(Exception e)
    {
    MessageBox.Show(e.ToString());
    }
    }
      

  6.   

    server发送一条 client接收一条,
    server向lan中所有的client发送广播!问题就在接收上 。。client只能接收到一条。
      

  7.   

    你的程序只执行一次receive,而receive只执行一次
    参看http://cht.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/net/TCPUDP/Chat.src&file=CS\chat.cs&font=3public static void Listener() {        //
            // The listener waits for data to come
            // and buffers it        Thread.Sleep(2000); // make sure client2 is receiving        Encoding    ASCII = Encoding.ASCII;        while(!m_Done) {
                IPEndPoint endpoint = null;
                Byte[] data = m_Client.Receive(ref endpoint);            String strData = ASCII.GetString(data);            if( strData.IndexOf(":@") > 0 ) {
                    //
                    // we received a termination indication
                    // now we have to decide if it is from
                    // our main thread shutting down, or
                    // from someone else
                    //
                    Char [] separators = {':'};
                    String [] vars = strData.Split(separators);                if( vars[0] == m_szHostName ) {
                        //
                        // this is from ourselves, therefore we
                        // end now
                        //
                        Console.WriteLine("正在關閉接聽項執行緒...");                    //
                        // this should have been done by main thread, but we
                        // do it again for safety
                        //
                        m_Done = true;
                    }
                    else {
                        //
                        // this is from someone else
                        //
                        Console.WriteLine("{0} 已經離開會話", vars[0]);
                    }
                }
                else {
                    //
                    // this is normal data received from others
                    // as well as ourselves
                    // check to see if it is from ourselves before
                    // we print
                    //
                    if(strData.IndexOf(":") > 0) {
                        Char [] separators = {':'};
                        String [] vars = strData.Split(separators);                    if( vars[0] != m_szHostName ) {
                            Console.WriteLine(strData);
                        }
                    }
                }
            }        Console.WriteLine("接聽項執行緒已經完成...");
            return;
        }