1

I go through this article example :

C# UDP Broadcast and receive example

But in this example the IP Address is taking the Local IP Address (ex: 192.168.xx.xx)

 var from = new IPEndPoint(0, 0); // Taking Local IP Address (ex: 192.168.xx.xx)

I want the clarification that, is it possible to give the particular IP Address like : "230.0.0.3"

I try to given the particular IP Address


        static void Main(string[] args)
        {
            int PORT = 7716;
            string ip = "230.0.0.3";
            UdpClient udpClient = new UdpClient();
            long udpip = Convert.ToInt64(ip.ToString());  // 'Input string was not in a correct format.'
            //udpClient.Client.Bind(new IPEndPoint(data, PORT));

            var from = new IPEndPoint(udpip, PORT); //IPAddress.Any

            if (from.Address.ToString() == "230.0.0.3" && from.Port.ToString() == "7716")
            {
                Task.Run(() =>
                {
                    while (true)
                    {
                        var recvBuffer = udpClient.Receive(ref from);
                        Console.WriteLine(Encoding.UTF8.GetString(recvBuffer));
                    }
                });
            }

           // var data = Encoding.UTF8.GetBytes("ABCD");
            //udpClient.Send(data, data.Length, "255.255.255.255", PORT);


            Console.ReadLine();
        }


While executing this program I'm getting this error

'Input string was not in a correct format.'

Note : Data should receive from IP Address : '230.0.0.3' and Port Number : 7716

Give me the suggestion. How can I achieve this ?

I'm new to the UPD Socket port concepts and logics.

ashok
  • 199
  • 1
  • 12
  • 1
    `var from = new IPEndPoint(IPAddress.Parse(ip), PORT)` – TheGeneral Sep 16 '21 at 07:44
  • "230.0.0.3" is not a valid int64 string representation. It is a "human readable" format. You would need to "translate" it first to make that conversion. For which there are convenience functions as metnioned by TheGeneral above. – Fildor Sep 16 '21 at 07:44
  • @TheGeneral, `var from = new IPEndPoint(IPAddress.Parse(ip), PORT)` . After changed the line code I'm getting this king of error **You must call the Bind method before performing this operation.** – ashok Sep 16 '21 at 07:56
  • I'm guessing, `udpClient.Client.Bind(from );` however someone with more knowledge in the `UdpClient` class can likely help you – TheGeneral Sep 16 '21 at 08:02

0 Answers0