C# question

Hello
how i can use this code with c# component?

   public void StartListener()
{
    bool done = false;

    UdpClient listener = new UdpClient(listenPort);
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

    try
    {
        while (!done)
        {
            Console.WriteLine("Waiting for broadcast");
            byte[] bytes = listener.Receive( ref groupEP);

            Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                groupEP.ToString(),
                Encoding.ASCII.GetString(bytes,0,bytes.Length));
        }

    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
    finally
    {
        listener.Close();
    }
}

That code needs some “using” statements:

using System.Net;
using System.Net.Sockets;
using System.Text;

and needs to be placed inside the

// <Custom additional code> 

// </Custom additional code>

Then the int “listenPort” is undefined, you can integrate is as a fixed number or by passing it from the void method like:

public void StartListener(int listenPort)

1 Like

Thank you , i will try that
I still learning and have no idea about c#

Are you using visual studio? It will usually add using statement for you automatically when they are missing, from the suggested fixes dialogue.

If you are programming a UDP listener, you’d probably need more than a C# component.

1 Like

No i don’t use visual studio, i found this script
I still trying , it’s just curiosity :smile: , i already use ghowl

i use ghowl , its udp listener is one component why i need more than one?

I believe he means you need more than a simple script component. It should rather be part of a custom plugin, where the component just register and output what has been received. You can do it in a single C# script component, but then you really need to write difficult code. Listing to UDP has to be implemented within another thread, otherwise you lock your definition from constantly scanning. This requires a quite intermediate understanding of coding. You also don’t want to reinitialize the listener everytime you rerun a solution, and you want to update the definition once it received.

1 Like

I know it is difficult , i never used c# before ,but it’s opportunity to learn even i will create it after 10 years :slight_smile:
Thank you all for useful informations

If you want to learn C# don’t start with the rather difficult things. Depending on your prior experience regarding coding, I would propose to learn the language up to the point where you reach the topic of concurrency. Async computing is one of the most difficult things, and even for advanced developer it is always challenging. Furthermore it is beneficial to advance in the field of debugging, especially for this sort of work. I know that the minimal example seems to be quite simple, but making it to work properly in GH is not.

3 Likes

Thank you for advice, yes i will but i will learn just what i need :slight_smile:
actually i stay with ghowl component