/// <summary> /// 获取当前的ip /// </summary> /// <returns>无线网卡所在的ip</returns> public static string GetIP() { // 获取本地主机名称列表 var hosts = NetworkInformation.GetHostNames(); // 筛选无线或以太网 var host = hosts.FirstOrDefault(h => { bool isIpaddr = (h.Type == Windows.Networking.HostNameType.Ipv4) || (h.Type == Windows.Networking.HostNameType.Ipv6); // 如果不是IP地址表示的名称,则忽略 if (isIpaddr == false) { return(false); } IPInformation ipinfo = h.IPInformation; // 71表示无线,6表示以太网 if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { return(true); } return(false); }); if (host != null) { return(host.DisplayName); //显示IP } return("没有检测到联网"); }
public async Task <StreamSocket> Client(HostName RemoteHost) { // 获取本机IP var HostNames = NetworkInformation.GetHostNames(); var LocalHost = HostNames.FirstOrDefault(h => { bool isIpaddr = h.Type == Windows.Networking.HostNameType.Ipv4; // 如果不是IP地址表示的名称,则忽略 if (isIpaddr == false) { return(false); } IPInformation ipinfo = h.IPInformation; // 71表示无线,6表示以太网 if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { return(true); } return(false); }); //尝试连接远程主机的1117端口 EndpointPair EndPoint = new EndpointPair(LocalHost, "", RemoteHost, "1117"); StreamSocket client = new StreamSocket(); await client.ConnectAsync(EndPoint); return(client); }
public IPInformation GetLocalization(string ip) { try { using (WebClient wc = new WebClient()) { var jsonData = wc.DownloadData($"http://ipinfo.io/{ip}/geo"); var json = Encoding.UTF8.GetString(jsonData); IPInformation ipInf = new IPInformation(); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)); DataContractJsonSerializer ser = new DataContractJsonSerializer(ipInf.GetType()); ipInf = ser.ReadObject(ms) as IPInformation; ms.Close(); return(ipInf); } } catch (InvalidOperationException ex) { throw new Exceptions.InvalidServerReponse(ex.Message); } catch (Exception ex) { throw ex; } }
private UInt32 Exe_SetStaticIPAddress(IPInformation info) { object[] args = new object[] { new string[] { info.IPAddressString }, new string[] { info.SubnetString } }; UInt32 ret = (UInt32)this.nic.InvokeMethod("EnableStatic", args); this.Renew(); return(ret); }
public MainPage() { InitializeComponent(); CurrentIP = IPHandler.CreateIPInformation().Result; CurrentIPBox.Text = CurrentIP.publicIP; IPHandler.IPList.Add(CurrentIP); IPHandler.AddSeveraltoListbox(ListOfIps, IPHandler.FormatIntoListbox(IPHandler.IPList)); IPHandler.SaveToFile(); }
private void RadioButton_Click(object sender, RoutedEventArgs e) { RadioButton rb = e.Source as RadioButton; if (rb.IsChecked == true) { IPInformation ipInfo = rb.DataContext as IPInformation; equip.AdminIPAddress = ipInfo.IP; } }
private async void MainPage_Loaded(object sender, RoutedEventArgs e) { socket = new DatagramSocket(); socket.Control.MulticastOnly = true; socket.MessageReceived += Socket_MessageReceived; await socket.BindServiceNameAsync("9000"); #region 获取本地IP var hosts = NetworkInformation.GetHostNames(); var host = hosts.FirstOrDefault(h => { bool isIpaddr = (h.Type == Windows.Networking.HostNameType.Ipv4) || (h.Type == Windows.Networking.HostNameType.Ipv6); if (isIpaddr == false) { return(false); } IPInformation ipinfo = h.IPInformation; if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { return(true); } return(false); }); if (host != null) { LocIp = host.DisplayName; txtLocIp.Text = "本机IP:" + host.DisplayName; //显示IP } #endregion #region 布局表格 if (await DevicesMethod.IsFirstUse() == true) { DevicesMethod.CreateLog();//设备文件丢失 } Obj = await DevicesMethod.GetDevices(); List <ViewDevModel> ObjView = new List <ViewDevModel>(); foreach (var item in Obj) { ObjView.Add(ModelConverter.DataToView(item)); } Controller.GridViewMethod.LayOutItem(gridView, ObjView); //布置控件 if (await IsFirstUse() == true) //队列文件丢失 { FileMethod.CreateTxt(); } #endregion foreach (var item in Obj)//布置已注册端口 { CreateLine(item.NetPort); } lblNetWorkName.Text = " " + InternetStatus.GetNetWorkName();//获取网络名称 //StatusBar S = StatusBar.GetForCurrentView(); //await S.HideAsync(); }
/// <summary> /// GetIpAddress Created by TheDarkKnight /// </summary> /// <returns></returns> public static string GetIP() { #if UNITY_STANDALONE_WIN || UNITY_IOS || UNITY_ANDROID || UNITY_EDITOR //获取说有网卡信息 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { //NetworkInterfaceType.Ethernet 本地连接 NetworkInterfaceType.Wireless80211无线连接 if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet || adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { if (adapter.Name.StartsWith("v")) { continue; } //获取以太网卡<a href="https://www.baidu.com/s?wd=%E7%BD%91%E7%BB%9C%E6%8E%A5%E5%8F%A3&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Ydm1TzP1NhmWw9nvn3nADd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHnvP10YnHRznjf1n1bznjnLrf" target="_blank" class="baidu-highlight">网络接口</a>信息 IPInterfaceProperties ip = adapter.GetIPProperties(); //获取单播地址集 UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses; foreach (UnicastIPAddressInformation ipadd in ipCollection) { if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork) { //Debug.Log(""+ ipadd.Address.ToString()); if (ipadd.Address.ToString().Contains("192.168")) { return(ipadd.Address.ToString()); } } } } } return(null); #elif UNITY_UWP var hosts = NetworkInformation.GetHostNames(); foreach (var h in hosts) { bool isIpaddr = (h.Type == Windows.Networking.HostNameType.Ipv4) || (h.Type == Windows.Networking.HostNameType.Ipv6); if (isIpaddr) { // 如果不是IP地址表示的名称,则忽略 IPInformation ipinfo = h.IPInformation; // 71表示无线,6表示以太网 if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { Debug.Log("本机的IP地址为:" + h.DisplayName); return(h.DisplayName); } } } return(null); #endif #if UNITY_WSA return(null); #endif }
public NIC(ManagementObject nic) { this.nic = nic; if ((bool)nic["DHCPEnabled"]) { this.backUp = null; } else { this.backUp = IPInformation.GetInstance(nic); } this.isChanged = false; }
private static string ToCIDR(string subnet) { int cidr; if (IPInformation.IsIPFormat(subnet)) { cidr = IPInformation.ToCIDR(subnet.Split(IPInformation.Dot).Select(oct => Byte.Parse(oct)).ToArray()); } else { cidr = -1; } return(cidr.ToString()); }
public static IPInformation GetInstance(string ip_subnet) { IPInformation instance; var ipDatas = ip_subnet.Split(IPInformation.Slash); if (ipDatas.Length == 2) { instance = IPInformation.GetInstance(ipDatas[0], ipDatas[1]); } else { instance = null; } return(instance); }
private async void initSocket() //asynchronous { var hosts = NetworkInformation.GetHostNames(); // 筛选无线或以太网 var host = hosts.FirstOrDefault(h => { bool isIpaddr = (h.Type == Windows.Networking.HostNameType.Ipv4) || (h.Type == Windows.Networking.HostNameType.Ipv6); // 如果不是IP地址表示的名称,则忽略 if (isIpaddr == false) { return(false); } IPInformation ipinfo = h.IPInformation; // 71表示无线,6表示以太网 if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { return(true); } return(false); }); if (host != null) { app.localIP = host.DisplayName; //显示IP } try { receiveUDP.MessageReceived += ReceiveUDP_MessageReceived; await receiveUDP.BindServiceNameAsync("1677"); } catch (Exception x) { ContentDialog dlg = new ContentDialog() { Title = "Socket Error", Content = "There was an error creating the sockets, the App will now close!", PrimaryButtonText = "OK", IsPrimaryButtonEnabled = true }; await dlg.ShowAsync(); Application.Current.Exit(); } }
public static IPInformation GetInstance(byte[] ip, int cidr) { IPInformation instance; if (cidr >= 0 && cidr <= 32) { UInt32 mask = 0xFFFFFFFF << (32 - cidr); byte[] subnet = new byte[4]; for (int index = subnet.Length - 1; index >= 0; index--) { subnet[index] = (byte)(mask & 0xFF); mask >>= 8; } instance = IPInformation.GetInstance(ip, subnet); } else { instance = null; } return(instance); }
public bool SetStaticIPAddress(IPInformation info) { bool ret; if (info != null) { ret = this.Exe_SetStaticIPAddress(info) == 0; if (ret) { this.isChanged = true; } else { this.RollBack(); } } else { ret = false; } return(ret); }
public static IPInformation GetInstance(byte[] ip, byte[] subnet) { IPInformation instance; if (ip == null || subnet == null) { instance = null; } if (ip.Length == 4 && subnet.Length == 4) { instance = new IPInformation(ip, subnet); } else if (ip.Length == 4 && subnet.Length == 1) { instance = IPInformation.GetInstance(ip, subnet[0]); } else { instance = null; } return(instance); }
//获取IP地址,返回布尔值 private bool GetIpAddress() { //https://docs.microsoft.com/en-us/uwp/api/windows.networking.connectivity.networkinformation.gethostnames //获取与本机相连接的主机名称 foreach (HostName localHost in NetworkInformation.GetHostNames()) { if (null == localHost) { return(false); } IPInformation ipInfo = localHost.IPInformation; //https://docs.microsoft.com/en-us/uwp/api/windows.networking.connectivity.ipinformation //属于connectivity命名空间 //Ianaxxx()获取一个代表网络接口类型的值,71代表WiFi地址 if (localHost.Type == HostNameType.Ipv4 && ipInfo.NetworkAdapter.IanaInterfaceType == 71) { host = localHost; Debug.Log(localHost); return(true); } } return(false); }
private static byte[] ToIPBytes(string ip) { byte[] ret; if (IPInformation.IsIPFormat(ip)) { ret = ip.Split(IPInformation.Dot) .Select(oct => Byte.Parse(oct)) .ToArray(); } else { byte cidr; if (Byte.TryParse(ip, out cidr)) { ret = new byte[] { cidr }; } else { ret = null; } } return(ret); }
//InitIPAndNameList private static void InitIPAndIPinfoList() { ipAndIPinfoList.Clear(); try { SqlDataReader dr = App.DBHelper.returnReader("SELECT IP_Index, IP_Address, IP_EquipID, IP_Mask, IP_GateWay, IP_Name, IP_IsDefaultIP FROM IPAddress"); while (dr.Read()) { //ipAndIPinfoList.Add(dr[0] as string, dr[1] as string); string strIP = dr["IP_Address"].ToString(); IpAddress mask = string.IsNullOrEmpty(dr["IP_Mask"].ToString()) ? null : new IpAddress(dr["IP_Mask"].ToString()); IpAddress gateway = string.IsNullOrEmpty(dr["IP_GateWay"].ToString()) ? null : new IpAddress(dr["IP_Mask"].ToString()); IPInformation info = new IPInformation(new IpAddress(strIP), Convert.ToInt32(dr["IP_EquipID"]), "", mask, gateway, dr["IP_Name"].ToString(), Convert.ToBoolean(dr["IP_IsDefaultIP"])); info.IpIndex = Convert.ToInt32(dr["IP_Index"]); ipAndIPinfoList.Add(strIP, info); } dr.Close(); } catch (Exception e) { MessageBox.Show("系统启动时初始化IP地址与IP信息列表出错\n" + e.Message); } }
public static IPInformation GetInstance(string ip, string subnet) { return(IPInformation.GetInstance(IPInformation.ToIPBytes(ip), IPInformation.ToIPBytes(subnet))); }
public bool SetStaticIPAddress(string ip_subnet) { return(this.SetStaticIPAddress(IPInformation.GetInstance(ip_subnet))); }
public bool doQuery(PropertyGrid grid) { input = (Input)grid.SelectedObject; ; this.res = geo.ResolveIP(input.Ip, "0"); grid.SelectedObject = this.res; return (this.res != null); }
async void weatherInit() { var ip = ""; var hosts = NetworkInformation.GetHostNames(); // 筛选无线或以太网 var host = hosts.FirstOrDefault(h => { bool isIpaddr = (h.Type == Windows.Networking.HostNameType.Ipv4) || (h.Type == Windows.Networking.HostNameType.Ipv6); // 如果不是IP地址表示的名称,则忽略 if (isIpaddr == false) { return(false); } IPInformation ipinfo = h.IPInformation; // 71表示无线,6表示以太网 if (ipinfo.NetworkAdapter.IanaInterfaceType == 71 || ipinfo.NetworkAdapter.IanaInterfaceType == 6) { return(true); } return(false); }); if (host != null) { ip = host.DisplayName; //显示IP } var city = ""; string url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; Uri uri = new Uri(url); Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); Windows.Web.Http.HttpResponseMessage httpResponseMessage = new Windows.Web.Http.HttpResponseMessage(); string httpResponseBody = ""; try { httpResponseMessage = await httpClient.GetAsync(uri); httpResponseMessage.EnsureSuccessStatusCode(); httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync(); } catch (Exception exception) { httpResponseBody = "ERROR " + exception.HResult.ToString("X") + "Message " + exception.Message; weather.Text = "离线模式"; return; } JsonReader jsonReader = new JsonTextReader(new StringReader(httpResponseBody)); while (jsonReader.Read()) { // Console.WriteLine(jsonReader.TokenType + "\t\t" + jsonReader.ValueType + "\t\t" + jsonReader.Value); if (jsonReader.Path == "data.city") { city = jsonReader.Value.ToString(); } } getWeather(city); }
private async void DebugAddIPToList_Click(object sender, RoutedEventArgs e) { IPInformation currentIP = await IPHandler.AddCurrentIPToList(); ListOfIps.Items.Add(IPHandler.FormatIntoListbox(currentIP)); }
/// <summary> /// 生成设备类信息,获取此设备的ip地址信息列表、接口信息列表、路由信息列表 /// </summary> /// <param name="strIP"></param> /// <param name="sysDescr"></param> /// <returns></returns> private Equipment GetEquipment(string strIP, string sysDescr, Equipment lastEquip) { Equipment equip = new Equipment(); equip.IpFirstGet = new IpAddress(strIP); if (App.ipAndIPinfoList.ContainsKey(strIP) && App.idAndEquipList.ContainsKey(App.ipAndIPinfoList[strIP].EquipIndex)) { //数据库中有此设备,从程序静态数据列表中取相应值 IPInformation ipInfo = App.ipAndIPinfoList[strIP]; int equipID = ipInfo.EquipIndex; Equipment tempEquip = App.idAndEquipList[equipID]; equip.Index = equipID; equip.Name = tempEquip.Name; equip.TypeName = tempEquip.TypeName; equip.TypeIndex = tempEquip.TypeIndex; equip.X = tempEquip.X; equip.Y = tempEquip.Y; if (string.IsNullOrEmpty(tempEquip.SysDescr)) { equip.SysDescr = sysDescr; } else { equip.SysDescr = tempEquip.SysDescr; } } else { //数据库中无此设备 equip.Index = equipNumNotInDB; equipNumNotInDB--; equip.TypeName = "其他"; equip.TypeIndex = 8; equip.Name = strIP; equip.SysDescr = sysDescr; //根据上个设备的位置来优化本设备位置 SetEquipPosition(equip, lastEquip); } if (!equip.GetIPAndInfoListFromSNMP(out tipMessage)) { AddMessage(tipMessage); AddMessage(string.Format("获取设备(ID:{0},Name:{1})IP地址信息列表时出现错误", equip.Index, equip.Name)); return(null); } AddMessage(string.Format("获取设备(ID:{0},Name:{1})IP地址信息列表", equip.Index, equip.Name)); //将本设备的ip地址和ip信息对列表加入到已发现地址和ip信息总列表中 foreach (KeyValuePair <IpAddress, IPInformation> pair in equip.IPAndInfoList) { if (pair.Key.ToString() == "3.248.1.2") { } if (ipAlreadyFindList.ContainsKey(pair.Key)) { AddMessage(string.Format("已发现IP地址列表中存在相同的IP地址{0},请确保网内无重复地址!\n已存在设备首要地址为{1},新添加地址所属设备首要地址为{2}", pair.Key.ToString(), ipAlreadyFindList[pair.Key].Equip.AdminIPAddress.ToString(), equip.AdminIPAddress.ToString())); } else { ipAlreadyFindList.Add(pair.Key, pair.Value); } } if (!equip.GetIFIDandIFInfoList(out tipMessage)) { AddMessage(tipMessage); AddMessage(string.Format("获取设备(ID:{0},Name:{1})接口信息列表时出现错误", equip.Index, equip.Name)); return(null); } AddMessage(string.Format("获取设备(ID:{0},Name:{1})接口信息列表", equip.Index, equip.Name)); if (!equip.GetIPAndRouteInfoList(out tipMessage)) { AddMessage(tipMessage); AddMessage(string.Format("获取设备(ID:{0},Name:{1})路由信息列表时出现错误", equip.Index, equip.Name)); return(null); } AddMessage(string.Format("获取设备(ID:{0},Name:{1})路由信息列表", equip.Index, equip.Name)); return(equip); }
public static IPInformation GetInstance(ManagementObject nic) { return(IPInformation.GetInstance(((string[])nic["IPAddress"])[0], ((string[])nic["IPSubnet"])[0])); }
public static IPInformation GetInstance(string ip, int cidr) { return(IPInformation.GetInstance(IPInformation.ToIPBytes(ip), cidr)); }