0

I'm implementing a udp discovery protocol wherein I broadcast a message to my local network and receive a message from custom hardware (or a simulator for the hardware) or multiple responses depending on how many instances are present on the network.

I have the broadcast working fine, however the receive portion has some issues: I see the broadcasts I send, but never the hardware responses. I know that the hardware is sending the broadcasts because I see them come in on wireshark when I monitor the traffic on that port.

What am I doing wrong?

IPEndPoint receiveIpEndPoint = new IPEndPoint(IPAddress.Any, 777);

private List<string> FindIPs() {
        string message = "{\"TCS\":{\"IP\":\"" + GetLocalIPAddress().ToString() + "\", \"Port\":777}}"; 
        byte[] tempBytes = ASCIIEncoding.ASCII.GetBytes(message);
        IPEndPoint broadcastIpEndPoint = new IPEndPoint(IPAddress.Broadcast, 777); 

        // list of UdpClients to send 
        List<UdpClient> sendClients = new List<UdpClient>();
        foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) {
            if ((!networkInterface.Supports(NetworkInterfaceComponent.IPv4)) ||
                (networkInterface.OperationalStatus != OperationalStatus.Up)) {
                continue;
            }

            IPInterfaceProperties adapterProperties = networkInterface.GetIPProperties();
            UnicastIPAddressInformationCollection unicastIPAddresses = adapterProperties.UnicastAddresses;
            IPAddress ipAddress = null;

            foreach (UnicastIPAddressInformation unicastIPAddress in unicastIPAddresses) {
                if (unicastIPAddress.Address.AddressFamily != AddressFamily.InterNetwork) {
                    continue;
                }
                ipAddress = unicastIPAddress.Address;
                break;
            }

            if (ipAddress == null) {
                continue;
            }

            UdpClient sendClient = new UdpClient(new IPEndPoint(ipAddress, 0));
            sendClients.Add(sendClient);
        }

        var udpreceive = new UdpClient(receiveIpEndPoint);
        udpreceive.BeginReceive(new AsyncCallback(ProcessUDPResponse), udpreceive);

        Log(message, LogManager.GetLogger("Sent UDP broadcast"));
        foreach (var udp in sendClients) {
            udp.EnableBroadcast = true;
            udp.Send(tempBytes, tempBytes.Length, broadcastIpEndPoint);
        }

        while(addresses.Count < 1) {

        }
        return addresses;
    }

    private void ProcessUDPResponse(IAsyncResult result) {
        UdpClient udp = result.AsyncState as UdpClient;
        string returnData = Encoding.ASCII.GetString(udp.EndReceive(result, ref receiveIpEndPoint));
        Console.WriteLine("**************           " + returnData.ToString());
        if (returnData.Contains("MAC")) {
            addresses.Add(receiveIpEndPoint.Address.ToString());
        }
        udp.BeginReceive(new AsyncCallback(ProcessUDPResponse), udp);
    }
senschen
  • 794
  • 10
  • 27
  • UDP is not required to receive it, thats the point its a stateless packet. It is not guaranteed to be received. – BugFinder Apr 10 '17 at 13:30
  • Like two weeks ago I wrote an example for UDP multicast. You might find it useful: http://stackoverflow.com/questions/43103714/c-sharp-broadcast-is-udp-message-listen-for-multiple-replies/43110412#43110412 – Jeroen van Langen Apr 10 '17 at 13:31
  • 2
    @BugFinder Right, but since I can see the response in wireshark, I know I'm getting it. I figured there must be something wrong in my implementation that's not letting me see it in my program. – senschen Apr 10 '17 at 13:34
  • Did you try : new IPEndPoint(IPAddress.Any, 777)? – jdweng Apr 10 '17 at 13:34
  • 1
    `UdpClient sendClient = new UdpClient(new IPEndPoint(ipAddress, 0));` port 0? – Jeroen van Langen Apr 10 '17 at 13:36
  • @jdweng For receiveIpEndPoint? yeah, edited that line in. – senschen Apr 10 '17 at 13:44
  • @JeroenvanLangen it doesn't seem to matter what I've got that port set to-- I've tried 0 and 777. – senschen Apr 10 '17 at 13:48
  • @JeroenvanLangen And thanks for the link, but that doesn't seem to be helping either. – senschen Apr 10 '17 at 13:56
  • Is it multicast (224.x.x.x to 239.x.x.x)? If another application is registered for the port you may not get receive it. The registering is first registered first get. The port is not forwarded unless you put it into a multicast group. You can check if other applications is registered for port using cmd.exe > netstat -a – jdweng Apr 10 '17 at 15:57
  • @jdweng The current version no, but I've tried that and it doesn't seem to make a difference. – senschen Apr 10 '17 at 16:00
  • Did you ever solve this? – Alex Johnson Sep 13 '18 at 18:35
  • @AlexJohnson This was quite a while ago, but if I remember right there were networking issues, something about how the pc or the program was set up. I know the final code looks pretty much like above. – senschen Sep 13 '18 at 19:17
  • @senschen Thank you. – Alex Johnson Sep 13 '18 at 19:19

0 Answers0