none
Question about UDP broadcast RRS feed

  • 問題

  • I use C# and Vistual Studio 2008 to write UDP broadcast.
    When PC only one network interface, it works fine.
    But when PC has two or more netowork interface, it can not receive any data.
    The code in following:

                        socket.ReceiveTimeout = 2000;
                        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                        string request = "M-SEARCH * HTTP/1.1\r\n" +
                        "HOST: 239.255.255.250:"+port+"\r\n" +
                        "ST:upnp:rootdevice\r\n" +
                        "MAN:\"ssdp:discover\"\r\n" +
                        "MX:3\r\n\r\n";

                        byte[] data = Encoding.ASCII.GetBytes(request);
                        IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast, port);
                        byte[] buffer = new byte[0x1000];

                        DateTime start = DateTime.Now;

                        socket.SendTo(data, ipe);

                        int length = 0;

                        do
                        {
                            length = socket.Receive(buffer);
                            length = buffer.Length;
                            string response = Encoding.UTF8.GetString(buffer, 0, length).ToLower();
                           
                        } while (length > 0);
                        socket.Close();

    When PC has two or more network interfaces, it will stop at "socket.Receive(buffer)" until timeout.

    2009年4月15日 下午 12:35

解答

  • mouseflute:
             You can use BeginReceiveFrom method  instead of Receive method to avoid the situation.
    • 已標示為解答 mouseflute 2009年4月23日 上午 02:28
    2009年4月15日 下午 02:17
    版主

所有回覆