简体   繁体   English

C#UdpClient.Receive()不能与多播一起使用,无论我做什么

[英]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. 我每10秒从本地网络上的另一台PC接收UDP数据包,可以在Wireshark中看到它们,但是应用程序卡在udpClient.Receive()行上。 The multicast group and port are the right values, checked in main() n+1 times. 多播组和端口是正确的值,在main()中检入n + 1次。 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) (我正在尝试接收服务器的信息,以便应用程序可以自动开始通过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. 正如我评论的那样,您的代码对我来说100%正常。 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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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