1

I'm trying to send a broadcast to contact other instances of my application. I'm running the code below in a Mono 3 Console program on the Mac (but also tried VS2012 on Windows). However, the message is never received. The receiver just sits there and blocks at the call

byte[] data = udpClient.Receive (ref endPoint);

EDIT:

I tried:

var recipient = new IPEndPoint (new IPAddress(new byte[] {192, 255, 255, 255}), 1667);

and also added

udpClient.EnableBroadcast = true;

to the sender. Still: not receiving anything. And that's it. Any proposals?

using System;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace NetworkServiceTest_Console
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Task.Run (() => Receiver ());
            Task.Run (() => {
                while(true)
                {
                    Sender ();
                    Thread.Sleep(1000);
                }
            });



            Console.ReadLine ();
        }

        static void Sender()
        {
            Console.WriteLine ("Sending...");
            var recipient = new IPEndPoint (IPAddress.Broadcast, 667);
            var udpClient = new UdpClient ();

            var data = Encoding.UTF8.GetBytes("Hallo world!");
            int bytesSent = udpClient.Send (data, data.Length, recipient);
            udpClient.Close ();
            Console.WriteLine ("{0} bytes sent", bytesSent);
        }

        static void Receiver()
        {
            Console.WriteLine ("Receiving...");
            var udpClient = new UdpClient ();
            var endPoint = new IPEndPoint(IPAddress.Any, 667);
            byte[] data = udpClient.Receive (ref endPoint);
            Console.WriteLine ("Received '{0}'.", Encoding.UTF8.GetString (data));
            udpClient.Close ();
        }
    }
}
Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • It hangs at that constructor call? Or does the program crash? Have you wrapped the call in a `try/catch` to see if it's throwing an exception? – Jim Mischel Oct 02 '13 at 19:30
  • @JimMischel I revised the question. Code was wrong. Now it's just not receiving anything. – Krumelur Oct 02 '13 at 19:32
  • What's between your application and the other instance(s) of your application? Is it the same host? Hosts on the same switch, router, LAN? It might be a network configuration thing and not a code thing. I'd verify via wireshark that packets are coming and going across the wire on port 667 on both hosts of your application. Broadcasts won't be forwarded outside of collision domains. – B L Oct 02 '13 at 19:46
  • You might find this useful: http://stackoverflow.com/q/687868/56778. Beyond that, I don't know. – Jim Mischel Oct 02 '13 at 20:38
  • @glace I try it on the same machine, so nothing in between, and also from the Mac to the Notebook on the same WLAN. – Krumelur Oct 03 '13 at 09:14

2 Answers2

3

This should do the trick:

static void Receiver( )
{
    Console.WriteLine( "Receiving..." );
    var udpClient = new UdpClient( 667 );
    var endPoint = new IPEndPoint( IPAddress.Any, 0 );
    byte[] data = udpClient.Receive( ref endPoint );
    Console.WriteLine( "Received '{0}'.", Encoding.UTF8.GetString( data ) );
    udpClient.Close( );
}

Provide the port number in the UdpClient constructor, and not in the Receive( ) method. The endpoint for the receive seems to be used like an out parameter, rather than an input parameter.

kaitsu
  • 66
  • 6
  • Yeah, the constructor specifies the local port to use (defaults to some random available port), the argument to `Receive` specifies the remote endpoint. – Jester Oct 03 '13 at 11:46
  • @kaitsu That solved it! And setting EnabledBroadcast = true is not required. I don't know what's that used for. – Krumelur Oct 03 '13 at 15:19
1

I see two possible problems there:

  1. You have to set UdpClient.EnableBroadcast to true. See the msdn.
  2. Ports under 1024 are privileged, you might need additional rights. Test with a port above 1024.

If it still doesn't work then use a network sniffer (e.g. wireshark) to analyze the traffic.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • Also 3rd problem is using IPAddress.Broadcast which is 255.255.255.255 almost always never works because software and hardware drops such a broad broadcast - try broadcast to a more specific subnet e.g. 192.255.255.255 – markmnl Oct 03 '13 at 04:11
  • @MarkMennell see my modified question. Nothing seems to help. – Krumelur Oct 03 '13 at 09:14