senschen
senschen

Reputation: 804

C# UdpClient not receiving packet seen by wireshark

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);
    }

Upvotes: 0

Views: 1027

Answers (0)

Related Questions