Question

I am working on a broadcast beacon in C# that is supposed to broadcast server information to all listening devices. The information sent will contain information like the URL of a WCF service, the namespace, a list of required arguments etc. What I have right now is a sender and receiver that can talk perfectly fine when they are on the same computer. However, once I put the sender on another computer than my receiver, the sender sends its message but my receiver never gets it. There are no exceptions being thrown, and the firewall is disabled on both machines.

http://codeidol.com/csharp/csharp-network/IP-Multicasting/What-Is-Broadcasting/ is where I got my code from.

Sender:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
            ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);
            byte[] data = Encoding.ASCII.GetBytes("This is a test message");
            sock.SendTo(data, iep);
            sock.Close();
        }
    }
}

Receiver:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPBroadcastReciever
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
            SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            sock.Bind(iep);
            EndPoint ep = (EndPoint)iep;
            Console.WriteLine("Ready to recieve");
            byte[] data = new byte[1024];
            int recv = sock.ReceiveFrom(data, ref ep);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("Received: {0} from: {1}", stringData, ep.ToString());
            sock.Close();
            Console.ReadLine();
        }
    }
}

Does anyone know of anything I am missing that would enable these two to talk on two different computers? They are on the same subnet (192.168.1.x)

Thanks Nick Long

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top