I'm trying to fetch some data, which is streamed via an UDP multicast Server. I've coded an C# WPF Application, where i can enter port and IP-address of server. The connection to the server is established sucessfully and i can receive multiple data packages (between 50-500 packages, varies over every try)
I'm calling the receive function via a dispatcher every 33ms. A longer time between the dispatcher event, does not solve the problem.
After a few seconds the UDPClient looses the connection, no data can be received and cannot be established any more.
Here is the function of the button establishing the connection and starts the dispatcher:
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;
}
Here is the receive function:
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());
}
}
Please let me know if somebody got any equal problem or a solution for my problem.