Вопрос

I am trying to fetch the IP address from the broadcast packets sent by a DSL modem and received on port 137 using UDP protocol. I am able to read the IP address when connectivity is present.

However, in unidentified network state, when my modem is configured for DHCP, and a specific range of IPs are allowed, and setting on my machine is auto-detect:

I get the default Local IP Address of the machine on reading the broadcast messages. That would be starting with 169.

Note: I am restarting the modem in order to receive the broadcast messages.

Socket sock = new Socket(AddressFamily.InterNetwork,
                        SocketType.Dgram, ProtocolType.Udp);
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, 137);
                sock.Bind(iep);
                EndPoint ep = (EndPoint)iep;
                int iLoopCount=0;
                while (iLoopCount <= 10000)
                {
                    Console.WriteLine("Ready to receive…");
                    byte[] data = new byte[1024];
                    int recv = sock.ReceiveFrom(data, ref ep);                    
                    string stringData = Encoding.Default.GetString(data, 0, recv);
                    Console.WriteLine("{1}: from: {0}",((IPEndPoint)ep).Address.ToString(),DateTime.Now.ToString());

                    iLoopCount++;
                   // Console.WriteLine(sock.RemoteEndPoint.ToString());
                }
                sock.Close();
                Console.Read();
Это было полезно?

Решение

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top