如何使用 C# UdpClient 检查数据是否发送到端口
我有一个程序,数据通过 UDP 从一台计算机发送到另一台计算机。问题是数据可能并不总是由发送程序发送,我希望仅当将某些内容发送到指定端口(本例中为 5000)时才启用接收程序的接收功能,否则当用户尝试接收数据时在使用 UdpClient
的端口上,程序崩溃。例如:
private const int listenPort = 5000;//receiving port
UdpClient listener = new UdpClient(listenPort);//udclient instance
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
public string received_data;
public byte[] receive_byte_array;
private void receiveButton_Click(object sender, RoutedEventArgs e)
{
receive_byte_array = listener.Receive(ref groupEP);
received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
textBox1.Text = received_data.ToString();
}
我的问题是,当没有发送数据并且用户单击主窗口上的 receiveButton
时,我的整个程序崩溃了。具体来说,问题在这里:receive_byte_array = Listener.Receive(ref groupEP);
我尝试通过将上面的代码行放在 try catch 语句中来解决问题,但即使这样程序也会崩溃!似乎只是尝试在 IPEndpoint 端口上接收数据,而没有任何数据会引发地狱。
关于如何首先检查数据是否发送到端口然后才允许用户接收数据的任何想法?提前致谢。
I have a program whereby Data is being sent from one computer to another via UDP. The problem is that data may not always be sent by the sending program and I want my receiving program's receive functionality to be enabled ONLY when something is being sent to a specified port (5000 in this case), otherwise when the user tries to receive data on the port using UdpClient
the program crashes. For example:
private const int listenPort = 5000;//receiving port
UdpClient listener = new UdpClient(listenPort);//udclient instance
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
public string received_data;
public byte[] receive_byte_array;
private void receiveButton_Click(object sender, RoutedEventArgs e)
{
receive_byte_array = listener.Receive(ref groupEP);
received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
textBox1.Text = received_data.ToString();
}
My problem here is when there is no data being sent and the User clicks receiveButton
on the main window my whole program crashes. To be specific the problem is here:receive_byte_array = listener.Receive(ref groupEP);
I've tried to fix the problem by putting the above line of code in a try catch statement but even then the program crashes! It seems merely trying to receive data on the IPEndpoint port when there is none raises hell.
Any ideas as to how I can first check if data is being sent to the port and only then allow the user to receive data? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的评论,我想说该程序正在冻结,因为它正在等待接收数据。您的用户界面冻结,因为您已经开始从 UI 线程同步侦听数据,并且它现在专注于侦听数据而不是重新绘制或处理输入。要解决此问题,请将侦听位放在单独的线程中或使用异步 BeginReceive方法接收数据。
based on your comment I'd say the program is freezing because it is waiting for data to receive. your user interface freezes because you've started listening for data synchronously from the UI thread and it is now preoccupied with listening for data and not repainting or processing input. to fix this put the listening bit in a separate thread or use the async BeginReceive method to receive the data.