我用udpclient接收消息的时候,总是在Receive时候就卡着不动了,也用了线程,不知道是什么原因,哪位大神给我看看
//发送代码
UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 1025);
byte[] buf = Encoding.Default.GetBytes("Are You Device?");
try
{
client.Send(buf, buf.Length, endpoint);
client.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//接收按钮
private void button2_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(RecvThread));
t.Start();
t.IsBackground = true;
}
//接收代码
public void RecvThread()
{
while (true)
{
UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 1025));
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
try
{
byte[] buf = client.Receive(ref endpoint);
string msg = Encoding.ASCII.GetString(buf);
//string strReceive = string.Format((endpoint as IPEndPoint).Address.ToString() + ":" + (endpoint as IPEndPoint).Port.ToString());
Console.WriteLine(msg.ToString());
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}