积极答复者
C# udp 问题?我要实现一直发送,我现在发送一次就结束了?

问题
-
private void udp()
{
textBox4.Text = string.Empty;
UdpClient udpclient = new UdpClient(Convert.ToInt32(textBox9.Text));
udpclient.Connect(textBox8.Text, Convert.ToInt32(textBox9.Text));
byte[] sendBytes = Encoding.Default.GetBytes(textBox5.Text);
udpclient.Send(sendBytes, sendBytes.Length);
IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpclient.Receive(ref ipendpoint);
string returnData = Encoding.Default.GetString(receiveBytes);
textBox4.Text += "【这条信息来自主机" + ipendpoint.Address.ToString() + ":" + ipendpoint.Port.ToString() + "】:" + returnData.ToString();
udpclient.Close();
}我要实现一直发送,我现在发送一次就结束了。
答案
-
你只调用一次Send方法当然就发送一次了,一直发送的话把发送的代码放到一个循环里面。
类似如下:
while(true)
{
udp();
Thread.Sleep(1000);//1秒发送一次
}
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 CaillenModerator 2014年8月18日 2:43
-
补充:用线程或者同步的Dictionary控制,每次需要时候发起一个线程,在线程里边做发送,防止界面假死。
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search and Download- 已标记为答案 CaillenModerator 2014年8月18日 2:43
全部回复
-
你只调用一次Send方法当然就发送一次了,一直发送的话把发送的代码放到一个循环里面。
类似如下:
while(true)
{
udp();
Thread.Sleep(1000);//1秒发送一次
}
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 CaillenModerator 2014年8月18日 2:43
-
补充:用线程或者同步的Dictionary控制,每次需要时候发起一个线程,在线程里边做发送,防止界面假死。
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search and Download- 已标记为答案 CaillenModerator 2014年8月18日 2:43