UDPClient 在收到多条消息后失去连接
UDPClient loosing connection after receiving multiple messages
我正在尝试获取一些数据,这些数据是通过 UDP 多播服务器流式传输的。我编写了一个 C# WPF 应用程序,我可以在其中输入服务器的端口和 IP 地址。与服务器的连接建立成功,我可以收到多个数据包(50-500个包之间,每次尝试都有所不同)
我每 33 毫秒通过调度程序调用接收函数。调度程序事件之间的时间更长,并不能解决问题。
几秒钟后,UDPClient 断开连接,无法接收任何数据,也无法再建立连接。
下面是按钮建立连接和启动调度程序的功能:
public int connectToArtWelder()
{
if (!checkIPAddress())
{
MessageBox.Show("Please enter a valid IP-Address.", "Wrong IP-Address", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
string iPString = tB_weldConIP.Text;
int port = Convert.ToInt32(tB_weldConPort.Text);
IPAddress iPAddress = IPAddress.Parse(iPString);
udpClient.Connect(iPAddress, port);
try
{
dispatcherTimer2.Tick += new EventHandler(Receive);
dispatcherTimer2.Interval = new TimeSpan(0, 0, 0, 0, 33);
dispatcherTimer2.Start();
}
catch
{
}
}
return 0;
}
这是接收函数:
private void Receive(object sender, EventArgs e)
{
try
{
int condition = udpClient.Available;
if (condition > 0)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
var asyncResult = udpClient.BeginReceive(null, null);
asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(10));
if (asyncResult.IsCompleted)
{
byte[] receiveBytes = udpClient.EndReceive(asyncResult, ref RemoteIpEndPoint);
double[] d = new double[receiveBytes.Length / 8];
// Do something with data
}
}
else
{
// The operation wasn't completed before the timeout and we're off the hook
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
如果有人遇到任何相同的问题或我的问题的解决方案,请告诉我。
解决方案:
我刚刚删除了
udpClient.Connect(ipAddress, port);
命令,现在运行超级顺利。
我正在尝试获取一些数据,这些数据是通过 UDP 多播服务器流式传输的。我编写了一个 C# WPF 应用程序,我可以在其中输入服务器的端口和 IP 地址。与服务器的连接建立成功,我可以收到多个数据包(50-500个包之间,每次尝试都有所不同)
我每 33 毫秒通过调度程序调用接收函数。调度程序事件之间的时间更长,并不能解决问题。
几秒钟后,UDPClient 断开连接,无法接收任何数据,也无法再建立连接。
下面是按钮建立连接和启动调度程序的功能:
public int connectToArtWelder()
{
if (!checkIPAddress())
{
MessageBox.Show("Please enter a valid IP-Address.", "Wrong IP-Address", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
string iPString = tB_weldConIP.Text;
int port = Convert.ToInt32(tB_weldConPort.Text);
IPAddress iPAddress = IPAddress.Parse(iPString);
udpClient.Connect(iPAddress, port);
try
{
dispatcherTimer2.Tick += new EventHandler(Receive);
dispatcherTimer2.Interval = new TimeSpan(0, 0, 0, 0, 33);
dispatcherTimer2.Start();
}
catch
{
}
}
return 0;
}
这是接收函数:
private void Receive(object sender, EventArgs e)
{
try
{
int condition = udpClient.Available;
if (condition > 0)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
var asyncResult = udpClient.BeginReceive(null, null);
asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(10));
if (asyncResult.IsCompleted)
{
byte[] receiveBytes = udpClient.EndReceive(asyncResult, ref RemoteIpEndPoint);
double[] d = new double[receiveBytes.Length / 8];
// Do something with data
}
}
else
{
// The operation wasn't completed before the timeout and we're off the hook
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
如果有人遇到任何相同的问题或我的问题的解决方案,请告诉我。
解决方案: 我刚刚删除了
udpClient.Connect(ipAddress, port);
命令,现在运行超级顺利。