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