2

tried to solve this alone for the past I don't even know but no googling will help me here, I would need some advice with this one. I am receiving UDP packets from another PC on my local network every 10 seconds, can see them in wireshark but the application is stuck on the udpClient.Receive() line. The multicast group and port are the right values, checked in main() n+1 times. Please suggest a solution if you have any idea that might help. Thanks.

(I'm trying to receive the server's information so that th application can automaticaly start to communicate vith it via TCP)

class MulticastListener {

    private UdpClient udpClient;
    private IPEndPoint remoteEndPoint;
    IPAddress multicastIP;
    private int port;

    public MulticastListener(ref IPAddress multicastIP, int port) {
        remoteEndPoint = new IPEndPoint(IPAddress.Any, port);

        this.multicastIP = multicastIP;
        this.port = port;
        udpClient = new UdpClient();
        udpClient.Client.Bind(remoteEndPoint);

    }

    public IPEndPoint GetServer() {

        try {
            udpClient.JoinMulticastGroup(multicastIP);
        } catch (ObjectDisposedException e) {
            Console.WriteLine("ERROR: The underlying socket has been closed!");
        } catch (SocketException e) {
            Console.WriteLine("ERROR: An error occurred when accessing the socket!");
        } catch (ArgumentException e) {
            Console.WriteLine("ERROR: The IP address is not compatible with the AddressFamily value that defines the addressing scheme of the socket!");
        }

        Byte[] serverInfoBytes = udpClient.Receive(ref remoteEndPoint);

        Stream stream = new MemoryStream(serverInfoBytes); //receives a serialised IPEndpoint object
        BinaryFormatter formatter = new BinaryFormatter();
        udpClient.Close();
        return (IPEndPoint)formatter.Deserialize(stream);
    }
}
TomPoczos
  • 95
  • 1
  • 9
  • Have you been able to successfully run the MSDN example: http://msdn.microsoft.com/en-us/library/ekd1t784(v=vs.110).aspx? – BradleyDotNET Apr 03 '14 at 22:49
  • Just quickly created an application and copy-pasted the code. Yes, it works. – TomPoczos Apr 03 '14 at 22:57
  • I'll look as well, but I would reccomend comparing the two and finding the critical difference. At least you have some code that you know works to compare against! – BradleyDotNET Apr 03 '14 at 23:00
  • 1
    I don't seem to be able to work out what the problem is. It's so frustrating because this is such a trivial example I cannot even imagine anything going wrong. And still it doesn't work. Based on what I've seen so far it should. Tried some modifications I came across too, e.g. setting the port i IPEndpoint to 0 or specify address family, always the same result. – TomPoczos Apr 04 '14 at 00:39
  • Have you tried passing in multicastIP into the IPEndPoint constructor? – FodderZone Apr 04 '14 at 00:41
  • I have now. Both with port set to 0 and the actual port value. Did not have too high hopes as IPAddress.Any should include it already, and indeed it did not work. But the IPEndPoint is indeed the only thing I can imagine causing the problem because program execution continues until the read(ref remoteEndPoint) method and get's stuck there waiting for datagrams. I actualy just tried it with the server's local address too (probably more chance of success there), but it still doesn't work. – TomPoczos Apr 04 '14 at 00:54
  • I can verify that your code works 100% as is when I use my test multicast sender. You said you wire-sharked it, but I would look at the sender. Are you sure you configured to the same multicast channel? One more thing to look at, if you are testing sender and receiver on the same machine, make sure you set the MulticastLoopback to true. – Chi_Town_Don Apr 04 '14 at 13:32

1 Answers1

1

As I commented, your code works fine for me 100% as is. I would check you are sending on the same subnet you are receiving on. Perhaps your sender is not configured to the right interface?

Perhaps it would help to try out a different sender, here is what I used to test:

    static void Main(string[] args)
    {
        //Configuration
        var interfaceIp = IPAddress.Parse("192.168.9.121");
        var interfaceEndPoint = new IPEndPoint(interfaceIp, 60001);
        var multicastIp = IPAddress.Parse("230.230.230.230");
        var multicastEndPoint = new IPEndPoint(multicastIp, 60001);

        //initialize the socket
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        socket.ExclusiveAddressUse = false;
        socket.MulticastLoopback = false;
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
        MulticastOption option = new MulticastOption(multicastEndPoint.Address, interfaceIp);
        socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option);

        //bind on a network interface
        socket.Bind(interfaceEndPoint);

        //initialize args for sending packet on the multicast channel
        var sockArgs = new SocketAsyncEventArgs();
        sockArgs.RemoteEndPoint = multicastEndPoint;
        sockArgs.SetBuffer(new byte[1234], 0, 1234);

        //send an empty packet of size 1234 every 3 seconds
        while (true)
        {
            socket.SendToAsync(sockArgs);
            Thread.Sleep(3000);
        }
    }
Chi_Town_Don
  • 111
  • 6