-1

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.

kremsi_02
  • 1
  • 2
  • 1
    _"UDPClient looses the connection"_ UDP is connectionless. So, what does that mean exactly? Do you get any exception or is it just not receiving anything anymore? – Fildor Jan 15 '20 at 12:15
  • 1
    `catch { }` = bad idea. – Fildor Jan 15 '20 at 12:16
  • Your usage of the APM (Asynchronous Programming Model) is also not super correct. If the receive operation exceeds your timeout, you seem to discard the result. – Fildor Jan 15 '20 at 12:28
  • The EndReceive need to be before the BeginReceive. End get the current message and Begin register the receive event so you can get more data. See msdn examples : https://learn.microsoft.com/en-us/dotnet/framework/network-programming/socket-code-examples – jdweng Jan 15 '20 at 12:30
  • "EndReceive need to be before the BeginReceive" but how should i react then on the asyncResult? – kremsi_02 Jan 15 '20 at 12:59

1 Answers1

0

SOLUTION: I've just removed the

udpClient.Connect(ipAddress, port);

command and now it's running super smoothly.

kremsi_02
  • 1
  • 2