C# 用Delegate处理UDP数据,怎么调试时无法接收数据呢?
现在的现象是,在VS2005中运行程序,能发出数据包,接收不到任何数据。如果直接运行exe文件,则没有任何问题。
namespace vpc_sender
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
UDP_Receiver udp = null;
private void buttonInit_Click(object sender, EventArgs e)
{
int myport = int.Parse(textMyPort.Text.Trim());
udp = new UDP_Receiver(myport);
udp.OnCommand += new UDP_Receiver.OnCommandDelegate(this.OnReceive);
}
void OnReceive(string s)
{
textRecText.Text = s;
textMsg.Text = udp.sMsg;
}
}
public class UDP_Receiver
{
UdpClient udp;
Thread rec_thread;
public String sCommand, sMsg;
bool bQuit;
public delegate void OnCommandDelegate(string s);
public OnCommandDelegate OnCommand;
public UDP_Receiver(int my_port)
{
try
{
udp = new UdpClient(my_port);
bQuit = false;
rec_thread = new Thread(receiver);
rec_thread.Start();
sMsg = "NET: Start";
OnCommand = new OnCommandDelegate(EmptyEvent);
}
catch
{
sMsg = "NET: Init error.";
udp = null;
}
}
void EmptyEvent(string s)
{ ; }
private void receiver()
{
string msg;
byte[] buf;
while (!bQuit)
{
try
{
IPEndPoint ip = null;
buf = udp.Receive(ref ip);
if (buf != null)
{
msg = Encoding.Default.GetString(buf);
OnCommand(msg);
sMsg = DateTime.Now.ToShortTimeString() + ":" + msg;
}
Thread.Sleep(100);
}
catch (Exception ex)
{
if (!bQuit)
Thread.Sleep(100);
sMsg = ex.Message;
}
}
bQuit = false;
}
}
} C# 调试 delegate UDP
补充:.NET技术 , C#