MarcFletcher / NetworkComms.Net

NetworkComms.Net is a high performance cross-platform network library written in C#.
http://networkcomms.net
Apache License 2.0
542 stars 242 forks source link

UDP Broadcast not received on Raspberry Pi 3 #36

Closed Peter-Krebs closed 5 years ago

Peter-Krebs commented 6 years ago

Thanks for making this library, I like the concept!

As the title suggests: I've tried to make a UDP broadcast work on two Raspberry Pi boxes, connected to the same router as my laptop. They seemingly cannot talk to each other using NetworkComms. There are no errors output or exceptions thrown while the application is running.

I need the Raspberrys to broadcast to each other. Is my usage of your library at fault here or do you have any pointers? How can I provide further info for debugging NetworkComms's part of things?

Versions
NetworkComms 3.0.3
Raspberrry Pi 3
.NET Framework 4.6.1 mono 4.6.2 OS: Raspbian Lite - Raspbian GNU/Linux 9 (stretch)

Source code

using NetworkCommsDotNet;
using NetworkCommsDotNet.Connections;
using NetworkCommsDotNet.Connections.UDP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace UdpExample
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkComms.AppendGlobalIncomingPacketHandler<string>("ChatMessage",
                (packetHeader, connection, incomingString) =>
                {
                    Console.WriteLine("\n  ... Incoming message from " +
                        connection.ToString() + " saying '" +
                        incomingString + "'.");
                });

            //Start listening for incoming UDP data
            Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 10000));

            Console.WriteLine("Type ESC to quit.");
            do
            {
                while (!Console.KeyAvailable)
                {

                    UDPConnection.SendObject("ChatMessage", "This is the broadcast test message!", new IPEndPoint(IPAddress.Broadcast, 10000));

                    Task.Delay(2000).Wait();
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

            NetworkComms.Shutdown();
        }
    }
}

SSH Session
When starting the program on two raspberries, they don't receive messages (console window does not print the received ChatMessage): 20181113-1-udp-10000 ssh-session

Wireshark sees broadcasts
Meanwhile... the broadcasts can be seen in Wireshark: 20181113-1-udp-10000 wireshark

Netcat works
A netcat session reveals that send/receive should work, so it's not likely a configuration issue: 20181113-1-udp-10000 netcat

Thanks in advance!

MarcFletcher commented 5 years ago

Couple of things for you to try:

  1. We have an included chat app example that you can configure to use UDP, I think it's in the AdvancedTest file. Does that work?
  2. Try using TCP to make sure the two devices can connect to one another and are not firewalled in some instances. Looks like you might be on windows, in which case make sure you have all firewalls turned off during the tests.

If that doesn't work let me know.

Peter-Krebs commented 5 years ago

Thanks for the pointers!

  1. The example worked partially. I selected Custom object, no compression, no encryption.
    The example works for UDP when entering the target IP and port.
    Sending a UDP broadcast did not work. See Screenshot below.
  2. I'm developing on a Windows machine, yes, but the code runs on two Raspberry Pis (www.raspberrypi.org) using Mono. The example with netcat from my OP shows both Raspberry Pi receive broadcast messages in general. There is no Windows machine involved during that. If there was a firewall issue I wouldn't have seen the broadcast packages with WireShark, but they're there. If the raspberry's had an active firewall (which I deactivated) the netcat broadcasts wouldn't have come through either, but they do.

We need the UDP broadcast for discovering other hosts. Manually looping through IP ranges would take forever in comparison and is not future-proof regarding IPv6 address ranges.

Screenshot: Output of the AdvancedSend example:
Sending 3 and "Three" works when explicitly stating IP + port.
Sending 4 and "Four" with a broadcast is not received on the other end.

20181122-1-udp-advancedsend ssh-session

Any ideas?

MarcFletcher commented 5 years ago

Yea, I take your points about the wireshark stuff. From experience UDP broadcasting can be problematic with some hardware. It might be for some reason the broadcast packets are not being sent up the network stack correctly on the pi's, but that's outside my area of expertise.

For reference if you're trying to broadcast between two .net native machines on the same network it works quite well (http://www.networkcomms.net/udp-broadcasting/). Perhaps if you have the machines that's another thing to try that might then point the finger directly at a mono issue or pi issue.

Peter-Krebs commented 5 years ago

The UDP broadcasts arrive when using netcat, so it's not blocked in any way.

Since I've posted the original issue I have adapted a low-level implementation based on this answer:
StackOverflow: How to do network discovery using UDP broadcast

This sends/receives UDP broadcasts flawlessly. So not truly a Mono or Pi issue.
But of course this does not have encryption and compression which I wanted to get to later.

If my code from the OP does not ring any bells for you in terms of what's wrong, maybe I need a bit of a re-think anyway what I could go test.

I'm on vacation for two weeks and will come back to this at some point.
If you don't have anything to add you can close the issue for now and I'll come back to the library later. Thanks for your time!

MarcFletcher commented 5 years ago

Am closing this as it's now stale and we never got to the bottom of this issue.

Many thanks for the discussion.