public void SNC_GetAP_Info() { List <AP_Information> old = new List <AP_Information> { }; byte[] data = new byte[4]; //broadcast data data[0] = 0xff; data[1] = 0x01; data[2] = 0x01; data[3] = 0x02; IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 1500); //braodcast IP address, and corresponding port NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); //get all network interfaces of the computer foreach (NetworkInterface adapter in nics) { // Only select interfaces that are Ethernet type and support IPv4 (important to minimize waiting time) if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet) { continue; } if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false) { continue; } try { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); foreach (var ua in adapterProperties.UnicastAddresses) { if (ua.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { //SEND BROADCAST IN THE ADAPTER Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //broadcast socket bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); bcSocket.ReceiveTimeout = 200; //receive timout 200ms IPEndPoint myLocalEndPoint = new IPEndPoint(ua.Address, 1500); bcSocket.Bind(myLocalEndPoint); bcSocket.SendTo(data, ip); //RECEIVE BROADCAST IN THE ADAPTER int BUFFER_SIZE_ANSWER = 1024; byte[] bufferAnswer = new byte[BUFFER_SIZE_ANSWER]; do { try { var recv = bcSocket.Receive(bufferAnswer); var redata = new byte[recv]; Array.Copy(bufferAnswer, 0, redata, 0, recv); if (recv == 36) { string dataS = Tools.ByteArrayToString(redata); int ip1 = Tools.ConvertHexToInt(dataS.Substring(10, 2)); int ip2 = Tools.ConvertHexToInt(dataS.Substring(12, 2)); int ip3 = Tools.ConvertHexToInt(dataS.Substring(14, 2)); int ip4 = Tools.ConvertHexToInt(dataS.Substring(16, 2)); string AP_IP = ip1 + "." + ip2 + "." + ip3 + "." + ip4; string MacAddress = dataS.Substring(18, 12); string DeviceName = dataS.Substring(38, 32); AP_Information mAP_Information = new AP_Information(); mAP_Information.AP_IP = AP_IP; mAP_Information.AP_MAC_Address = MacAddress; mAP_Information.AP_Name = Tools.ConvertHexToString(DeviceName); old.Add(mAP_Information); } } catch (Exception e) { // //Console.Write(e.ToString() + Environment.NewLine); // bcSocket.Close(); break; } } while (bcSocket.ReceiveTimeout != 0); //fixed receive timeout for each adapter that supports our broadcast bcSocket.Close(); } } } catch { } } ApScanEventArgs obj = new ApScanEventArgs(); obj.data = old; onApScanEvent(this, obj); }
public void SNC_GetAP_Info() { /* try * { * * Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); * // socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); * // socket.ReceiveTimeout = 200; * // socket.Connect(new IPEndPoint(IPAddress.Any, 0)); * var serverFullAddr = new IPEndPoint(IPAddress.Parse("10.10.100.100"),48899);//设置IP,端口 //"10.10.100.100"),8899 OK * Console.WriteLine("aaaaaaaaaaaaaaa"); * var sfasfs = new IPEndPoint(IPAddress.Broadcast,48899);//设置IP,端口 * //byte[] numArray = new byte[] { 0x61 ,0x64, 0x6D, 0x69, 0x6E, 0x0D, 0x0A }; * //byte[] numArray = new byte[] { 255, 0, 1, 1, 2}; * // byte[] numArray = Encoding.ASCII.GetBytes("www.usr.cn"); * // byte[] numArray = new byte[] { 0x55, 0xFD, 0xAA, 0x00, 0x03, 0x61, 0x00, 0xCC, 0x2D }; * byte[] numArray = new byte[] { 0x77, 0x77, 0x77, 0x2E, 0x75, 0x73, 0x72, 0x2E, 0x63, 0x6E }; * socket.ReceiveTimeout = 1000; * // byte[] numArray = new byte[] { 0x41, 0x54, 0x2B, 0x4E, 0x45, 0x54, 0x50, 0x0a}; * for (var a = 0; a < numArray.Length; a++) * { * Console.WriteLine("numArray" + a + ":" + numArray[a]); * } * * * Console.WriteLine("aaaaaaaaaaaaaaa"); * * // Connect to the remote endpoint. * // socket.BeginConnect(serverFullAddr, * // new AsyncCallback(ConnectCallback), socket); * // connectDone.WaitOne(); * * //socket.Connect(serverFullAddr); * socket.Bind(serverFullAddr); * * Console.WriteLine("BBBBBBBBBBBBB"); * // socket.Send(numArray); * socket.Send(numArray); * byte[] numArray1 = new byte[1024]; * Console.WriteLine("aaaaaaaaaaa"); * int num = socket.Receive(numArray1); * * Console.WriteLine("socket.Receive" + num); * * byte[] numArray2 = new byte[num]; * * Console.WriteLine("numArray2" + numArray2); * Array.Copy(numArray1, 0, numArray2, 0, num); * string strss = Tools.ByteArrayToString(numArray2); * Console.WriteLine("strss" + strss); * * * } * catch (Exception ee) * { * Console.WriteLine("exception" + ee); * }*/ List <AP_Information> aPInformations = new List <AP_Information>(); byte[] numArray = new byte[] { 255, 1, 1, 2 }; //byte[] numArray = new byte[] { 255, 0, 1, 1, 2 }; //byte[] numArray = new byte[] { 0x77, 0x77, 0x77, 0x2E, 0x75, 0x73, 0x72, 0x2E, 0x63, 0x6E }; // byte[] numArray = new byte[] { 0x61 ,0x64, 0x6D, 0x69, 0x6E, 0x0D, 0x0A }; IPEndPoint pEndPoint = new IPEndPoint(IPAddress.Broadcast, 1500); NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); for (int i = 0; i < (int)allNetworkInterfaces.Length; i++) { NetworkInterface networkInterface = allNetworkInterfaces[i]; if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet && networkInterface.Supports(NetworkInterfaceComponent.IPv4)) { try { foreach (UnicastIPAddressInformation unicastAddress in networkInterface.GetIPProperties().UnicastAddresses) { if (unicastAddress.Address.AddressFamily != AddressFamily.InterNetwork) { continue; } IPEndPoint BindPoint = new IPEndPoint(unicastAddress.Address, 1500); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); socket.ReceiveTimeout = 200; socket.Bind(BindPoint); socket.SendTo(numArray, pEndPoint); byte[] numArray1 = new byte[1024]; do { try { int num = socket.Receive(numArray1); byte[] numArray2 = new byte[num]; Array.Copy(numArray1, 0, numArray2, 0, num); Console.WriteLine("strrss" + numArray2); if (num == 36) { string str = Tools.ByteArrayToString(numArray2); int num1 = Tools.ConvertHexToInt(str.Substring(10, 2)); int num2 = Tools.ConvertHexToInt(str.Substring(12, 2)); int num3 = Tools.ConvertHexToInt(str.Substring(14, 2)); int num4 = Tools.ConvertHexToInt(str.Substring(16, 2)); string str1 = string.Concat(new object[] { num1, ".", num2, ".", num3, ".", num4 }); string str2 = str.Substring(18, 12); string str3 = str.Substring(38, 32); AP_Information aPInformation = new AP_Information() { AP_IP = str1, AP_MAC_Address = str2, AP_Name = Tools.ConvertHexToString(str3) }; aPInformations.Add(aPInformation); } } catch (Exception exception) { Console.WriteLine("exception" + exception); break; } }while (socket.ReceiveTimeout != 0); socket.Close(); } } catch { } } } Tools.ApScanEventArgs apScanEventArg = new Tools.ApScanEventArgs() { data = aPInformations }; this.onApScanEvent(this, apScanEventArg); }
public void SNC_GetAP_Info() { List <AP_Information> old = new List <AP_Information> { }; /* byte[] data = new byte[4]; //broadcast data * data[0] = 0xff; * data[1] = 0x01; * data[2] = 0x01; * data[3] = 0x02;*/ // byte[] data = new byte[] { 0x77, 0x77, 0x77, 0x2E, 0x75, 0x73, 0x72, 0x2E, 0x63, 0x6E }; byte[] data = Encoding.ASCII.GetBytes("HF-A11ASSISTHREAD"); IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 48899); //braodcast IP address, and corresponding port NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); //get all network interfaces of the computer foreach (NetworkInterface adapter in nics) { // Only select interfaces that are Ethernet type and support IPv4 (important to minimize waiting time) if (adapter.NetworkInterfaceType != NetworkInterfaceType.Wireless80211) { continue; } if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false) { continue; } try { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); foreach (var ua in adapterProperties.UnicastAddresses) { if (ua.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { //SEND BROADCAST IN THE ADAPTER Console.WriteLine("FFFFFFFFFFFFF" + ua.Address); Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //broadcast socket bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); bcSocket.ReceiveTimeout = 200; //receive timout 200ms IPEndPoint myLocalEndPoint = new IPEndPoint(ua.Address, 48899); bcSocket.Bind(myLocalEndPoint); bcSocket.SendTo(data, ip); //RECEIVE BROADCAST IN THE ADAPTER int BUFFER_SIZE_ANSWER = 1024; byte[] bufferAnswer = new byte[BUFFER_SIZE_ANSWER]; do { try { IPEndPoint sssss = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sssss); var recv = bcSocket.Receive(bufferAnswer); var redata = new byte[recv]; Array.Copy(bufferAnswer, 0, redata, 0, recv); Console.WriteLine("recv" + recv); Console.WriteLine("redata" + redata); Console.WriteLine("SSSSSSSSSSS" + Tools.ByteArrayToString(redata)); Console.WriteLine("AAAAAAAAAAA" + Tools.ConvertHexToString(Tools.ByteArrayToString(redata))); if (recv == 27) { string str = Tools.ByteArrayToString(redata); Console.WriteLine("str" + str); string data2 = Tools.ConvertHexToString(str); string[] IPMAC = data2.Split(','); string str1 = IPMAC[0]; string str2 = IPMAC[1]; AP_Information mAP_Information = new AP_Information(); mAP_Information.AP_IP = str1; mAP_Information.AP_MAC_Address = str2; old.Add(mAP_Information); } } catch (Exception e) { // Console.Write(e.ToString() + Environment.NewLine); // bcSocket.Close(); break; } } while (bcSocket.ReceiveTimeout != 0); //fixed receive timeout for each adapter that supports our broadcast bcSocket.Close(); } } } catch { } } ApScanEventArgs obj = new ApScanEventArgs(); obj.data = old; onApScanEvent(this, obj); }