0

I am trying to write code the receives broadcast messages coming from another app. using wireShark, I can see that they are sent to 255.255.255.255:300.

I tried the following code:

                IPEndPoint ServerEndPoint = new IPEndPoint(IPAddress.Any, 3000);
                Socket WinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                WinSocket.Bind(ServerEndPoint);
                byte[] data = new byte[10];
                Console.Write("Waiting for client");
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint Remote = (EndPoint)(sender);
                int recv = WinSocket.ReceiveFrom(data, ref Remote);
                Console.WriteLine("Message received from {0}:", Remote.ToString());
                string str = Encoding.ASCII.GetString(data, 0, recv);

but when my app reached to the "bind" command there is an error message: "Only one usage of each socket address (protocol/network address/port) is normally permitted"

if I change the port there is no error but I catch nothing.

Please advice

Udi Raz
  • 21
  • 2
  • The source and destination ports do not need to be the same. In fact, you should use source port `0` to allow the system to assign an ephemeral source port. Only the destination port needs to be a fixed port number. – Ron Maupin Sep 03 '20 at 16:02
  • You do not need both port 0 and 3000 which is giving the error. I would start with Putty and see if you receive the messages in putty. Also from cmd.exe>Netstat -a and see if another application is register to receive the port number. It is possible another app is registered for the port so your app is not receiving the messages. – jdweng Sep 03 '20 at 16:10
  • Does this answer your question? [UdpClient receive on broadcast address](https://stackoverflow.com/questions/746519/udpclient-receive-on-broadcast-address) – Jason Sep 03 '20 at 16:44

0 Answers0