简体   繁体   中英

C# UdpClient.Receive() not working with multicast no matter what I do

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM