Linux没有收到广播消息:C#

时间:2015-01-02 04:34:06

标签: c# linux mono udp

在树莓派上没有收到一些广播消息的问题。我可以从Linux机器发送它们,并在Windows机器上接收它们,但无论linux机器是否接收到从广播发送数据包的位置。我已经对此进行了长时间的故障排除,但仍无法找到原因。在Windows和Linux机器上使用mono。

只是忽略与mac地址有关的一切,这只是我在套接字之间发送的信息

发件人代码:

public string[] refresh() 
    {
        this.mac_addresses.Clear ();

        UdpClient sock = new UdpClient ();
        IPEndPoint iep = new IPEndPoint (IPAddress.Broadcast, 15001);

        byte[] data = Encoding.ASCII.GetBytes("PING");

        sock.Send (data, data.Length, iep);

        Stopwatch s = new Stopwatch ();
        s.Start ();
        while (s.Elapsed < TimeSpan.FromSeconds (5) || sock.Available != 0) {
            if (sock.Available != 0) {
                byte[] recieve = sock.Receive (ref iep);
                string recieve_string = Encoding.ASCII.GetString (recieve);
                this.mac_addresses.Add (recieve_string);
            }
        }
        return this.mac_addresses.Distinct ().ToArray ();
    }

接收人代码:

public static void Main (string[] args)
    {
        string mac_address = GetMacAddress ();
        Console.WriteLine (mac_address);

        UdpClient sock = new UdpClient (15001);
        IPEndPoint sender = new IPEndPoint (IPAddress.Any, 0); 

        byte[] data = new byte[1024];
        string string_data;

        while (true) {
            data = sock.Receive (ref sender);
            string_data = Encoding.ASCII.GetString (data);


            if (string_data == "PING") {
                Console.WriteLine ("RECIEVED PACKET");
                byte[] mac_data = Encoding.ASCII.GetBytes (mac_address);
                sock.Send (mac_data, mac_data.Length, sender);
            }
        }
    }

0 个答案:

没有答案