tags:

views:

60

answers:

3

I am trying to set up auto discovery using UDP multicasting, and am using some sample code from the internet. this seems to work ok when I run the client and the server on the same machine, but when I run them on different machines, either with a machine running in a VM on my machine (virtualBox) or on other 'real' machines on the network then the other machines never seem to receive the messages being broadcast.

After some googling it seems the likely culprit would be the router (SpeedTouch 780) which might be dropping the packets. How can I check if this is the case? Are their other things which I can check to try and track down the problem? Migth it be somethign else entirely?

teh codez:

server code

using System;
using System.Net.Sockets;
using System.Text;

internal class StockPriceMulticaster
    {
    private static string[] symbols = {"ABCD", "EFGH", "IJKL", "MNOP"};

    public static void Main ()
        {
        using (UdpClient publisher = new UdpClient ("230.0.0.1", 8899))
            {
            Console.WriteLine ("Publishing stock prices to 230.0.0.1:8899");
            Random gen = new Random ();
            while (true)
                {
                int i = gen.Next (0, symbols.Length);
                double price = 400*gen.NextDouble () + 100;
                string msg = String.Format ("{0} {1:#.00}", symbols, price);
                byte[] sdata = Encoding.ASCII.GetBytes (msg);
                publisher.Send (sdata, sdata.Length);
                System.Threading.Thread.Sleep (5000);
                }
            }
        }
    }

and the client:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class StockPriceReceiver{
    public static void Main(){
        UdpClient subscriber = new UdpClient(8899);
        IPAddress addr = IPAddress.Parse("230.0.0.1");
        subscriber.JoinMulticastGroup(addr);
        IPEndPoint ep = null;
        for(int i=0; i<10;i++){
            byte[] pdata = subscriber.Receive(ref ep);
            string price = Encoding.ASCII.GetString(pdata);
            Console.WriteLine(price);
        }
        subscriber.DropMulticastGroup(addr);
    }
}

EDIT

So it seems that it is publishing the UDP packets on the VirtualBox host only network interface for some reason rather than the wireless network that all the machines are connected to. Just need to figure out how to make it not do that... So added the resolution in an answer instead...

A: 

I'd try Wireshark.

Ladlestein
yeah I'd started the installation of that already. I was hoping for some other options as well. oh well we'll see I suppose...
Sam Holder
This identified the issue, data going to the wrong network interface.
Sam Holder
A: 

There are a few issues to look into here.

The first is: are you sure multicast is the best way of doing this? I think broadcast would serve you better.

The other is: routers generally don't forward multicast or broadcast, switches and hubs do.

Take a look at the following two questions: http://stackoverflow.com/questions/2046275/why-are-udp-multicast-packets-not-being-recieved and http://stackoverflow.com/questions/3068497/udp-multicast-over-the-internet

EDIT:

When you create your UdpClient you can specify which local endpoint you will be sending from. http://msdn.microsoft.com/en-us/library/k227d11f.aspx

Guge
issue was data going to the wrong interface. not sure how I can make it go to the right one though...
Sam Holder
I have edited my answer to include something about this.
Guge
A: 

So the issue turned out to be that as I had more than 1 active network connection it was choosing one and using that and that was causing the UDP packets to be sent out on a different network connection that the client was listening on. As i had installed Virtual box it had installed and activated the VirtualBox Host-only network adapter, so that host only network connections could be supported. When I switched VirtualBox over to host only mode the packets started to be received. Disabling the VirtualBox adapter and switching back to a bridged connection also worked.

Sam Holder