巨大的问题-----udp client接收广播的问题 。

lucbesson 2004-12-05 01:57:15
private void Receive()
{
// **STEP 1**创建实例
UdpClient receive= new UdpClient(8080);

IPEndPoint RemoteIpEndPoint=null;

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
this.richTextBox1.AppendText(returnData);
问题在这里,当第一次发送广播的时候,client 接收到信息,并显示出来,
第二次发送广播的时候,richTextBox1里面并没有追加我第二发送的信息,还是第一次的,
该怎么改写this.richTextBox1.AppendText这一句?
this.richTextBox1.AppendText(DateTime.Now+"\r"+returnData+"\n");改成这样也不行 !

问题2
CLIENT 启动后,界面有时候不显示,只有在接收到信息的时候才显示。怎么回事 ?

...全文
318 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
2002pine 2004-12-06
  • 打赏
  • 举报
回复
你的程序只执行一次receive,而receive只执行一次
参看http://cht.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/net/TCPUDP/Chat.src&file=CS\chat.cs&font=3

public 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;
}
cnhgj 2004-12-06
  • 打赏
  • 举报
回复
用多线程控制,创建一线程处理接收数据的方法。该方法弄个死循环,这样才可以不断接收数据
lucbesson 2004-12-06
  • 打赏
  • 举报
回复
server发送一条 client接收一条,
server向lan中所有的client发送广播!

问题就在接收上 。。。。。client只能接收到一条。
lucbesson 2004-12-06
  • 打赏
  • 举报
回复
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());
}
}
2002pine 2004-12-06
  • 打赏
  • 举报
回复
用多线程控制,创建一线程处理接收数据的方法。该方法弄个死循环,这样才可以不断接收数据

你是不是点一下udpServer发一条,然后再接收一条?

所以你的方法是不对的,不能每次都new 一个udpClient,这样会造成在发送时,这个udpClent不存在,
当然它生成时,就不可能接收到了.
你可以把它设为全局的,
lucbesson 2004-12-05
  • 打赏
  • 举报
回复
再顶。。。。。

问题是:接收不到第二次的广播,为什么 ?
xiaoslong 2004-12-05
  • 打赏
  • 举报
回复
再帮你顶一次
xxdneu 2004-12-05
  • 打赏
  • 举报
回复
看一看quickstart里的udp chat例子吧,

我越解释越你越蒙
lucbesson 2004-12-05
  • 打赏
  • 举报
回复
蒙了。。。。。
xxdneu(xxd) 联系一下啊,
问点问题,好不好?
xxdneu 2004-12-05
  • 打赏
  • 举报
回复
兄弟udpclient 的 receive方法本身是阻塞方法,所以你写在死循环里是没有问题的,如果你想有个退出条件你可以写到while的条件里

另外你这种接收网络消息的东西肯定是需要开一个新的线程的,你要写在主线程里肯定是行不通的,因为receive要阻塞当前线程,所以你写的那代码也就是接收到了一个消息然后主线程才开始向前运行的,所以你的界面开始出不来,这也是你的第二个问题,同样是由于你receive没有用正确。

唉,好心给你解释,被你认为是捣乱... 郁闷
zhongwanli 2004-12-05
  • 打赏
  • 举报
回复
不好意思,不知道.

xxdneu 2004-12-05
  • 打赏
  • 举报
回复
你那种写法也只能接收到第一次数据,并不是没有追加第二次发送的信息,而是你根本就没接收到
所以你接收需要写到循环里

“楼上的你们在干吗 ?”


什么叫我们在干吗?????

lucbesson 2004-12-05
  • 打赏
  • 举报
回复
楼上的你们在干吗 ?

xiaoslong 2004-12-05
  • 打赏
  • 举报
回复
帮你顶一下
xxdneu 2004-12-05
  • 打赏
  • 举报
回复
接收信息需要在循环里进行,而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());
}
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧