UdpClient UDPserver = new UdpClient(123);
IPEndPoint IP = null;
try
{
while (true)
{
UDPserver.Receive(ref IP);
bytes = Encoding.UTF8.GetBytes("您好!");
UDPserver.Send(bytes, bytes.Length,IP);
}
}
catch (Exception e)
{
}
finally //确保线程退出时关闭!!
{
UDPserver.Close();
}
IPEndPoint IP = null;
try
{
while (true)
{
UDPserver.Receive(ref IP);
bytes = Encoding.UTF8.GetBytes("您好!");
UDPserver.Send(bytes, bytes.Length,IP);
}
}
catch (Exception e)
{
}
finally //确保线程退出时关闭!!
{
UDPserver.Close();
}
这个就是调两次的原因,你这里new了一个静态form。但是在程序运行的时候,main函数又会new一个form.所以你的程序跑起来后是有两个form1的。
两个form1意外着你有两个线程
至于如果你不知道我说的main在哪,那么请看你工程目录下的Program.CS文件