C#获取本地IP地址,内网+外网方法

 1 #region 获取内、外网Ip
 2
 3         /// <summary>
 4         /// 获取本地ip地址,优先取内网ip
 5         /// </summary>
 6         public static String GetLocalIp()
 7         {
 8             String[] Ips = GetLocalIpAddress();
 9
10             foreach (String ip in Ips) if (ip.StartsWith("10.80.")) return ip;
11             foreach (String ip in Ips) if (ip.Contains(".")) return ip;
12
13             return "127.0.0.1";
14         }
15
16         /// <summary>
17         /// 获取本地ip地址。多个ip
18         /// </summary>
19         public static String[] GetLocalIpAddress()
20         {
21             string hostName = Dns.GetHostName();                    //获取主机名称
22             IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址
23
24             string[] IP = new string[addresses.Length];             //转换为字符串形式
25             for (int i = 0; i < addresses.Length; i++) IP[i] = addresses[i].ToString();
26
27             return IP;
28         }
29
30         /// <summary>
31         /// 获取外网ip地址
32         /// </summary>
33         public static string GetExtenalIpAddress_0()
34         {
35             string IP = "未获取到外网ip";
36             try
37             {
38                 //从网址中获取本机ip数据
39                 System.Net.WebClient client = new System.Net.WebClient();
40                 client.Encoding = System.Text.Encoding.Default;
41                 string str = client.DownloadString("http://1111.ip138.com/ic.asp");
42                 client.Dispose();
43
44                 //提取外网ip数据 [218.104.71.178]
45                 int i1 = str.IndexOf("["), i2 = str.IndexOf("]");
46                 IP = str.Substring(i1 + 1, i2 - 1 - i1);
47             }
48             catch (Exception) { }
49
50             return IP;
51         }
52
53         /// <summary>
54         /// 获取外网ip地址
55         /// </summary>
56         public static string GetExtenalIpAddress()
57         {
58             String url = "http://hijoyusers.joymeng.com:8100/test/getNameByOtherIp";
59             string IP = "未获取到外网ip";
60             try
61             {
62                 //从网址中获取本机ip数据
63                 System.Net.WebClient client = new System.Net.WebClient();
64                 client.Encoding = System.Text.Encoding.Default;
65                 string str = client.DownloadString(url);
66                 client.Dispose();
67
68                 if (!str.Equals("")) IP = str;
69                 else IP = GetExtenalIpAddress_0();
70             }
71             catch (Exception) { }
72
73             return IP;
74         }
75
76         #endregion

为了获取IP 我寻找了很多方法 大部分还是获得全部IP

//获取全部IP  string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);

//获取全部IP
IPHostEntry myEntry = Dns.GetHostEntry(Dns.GetHostName());
myEntry.AddressList.FirstOrDefault<IPAddress>(e => e.AddressFamily.ToString().Equals("InterNetwork")).ToString();

        /// <summary>
        /// 获取本地IP地址信息
        /// </summary>
        void GetAddressIP()
        {
            ///获取本地的IP地址
            string AddressIP = string.Empty;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress.ToString();
                }
            }
            string Text = AddressIP;
        }

原文地址:https://www.cnblogs.com/ning-xiaowo/p/12696959.html

时间: 2024-08-05 10:14:37

C#获取本地IP地址,内网+外网方法的相关文章

Linux下编程获取本地IP地址的常见方法

转载于:http://blog.csdn.net/k346k346/article/details/48231933 在进行linux网络编程时,经常用到本机IP地址.本文罗列一下常见方法,以备不时之需. 获取本机IP地址,是一个相当灵活的操作,原因是网络地址的设置非常灵活而且都是允许用户进行个性化设置的.比如一台计算机上可以有多块物理网卡或者虚拟网卡,一个网卡上可以绑定多个IP地址,用户可以为网卡设置别名,可以重命名网卡.用户计算机所在网络拓扑结构未知,主机名设置是一个可选项,并且同样可以为一

Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip

获取本地 ip 地址,mac,通过域名获取对应的 ip, 是网络编程可能遇到的比较常见的操作了,所以总结如下(封装了3个函数), 直接上代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <netdb.h> #include <net/if.h> #inc

Android获取本地IP地址,Ipv4地址检查,Ipv6地址检查

/** * 获取本地IP地址 * @author YOLANDA * @return */ public static String getLocalIPAddress() { String ipAddress = ""; try { Enumeration<NetworkInterface> netfaces = NetworkInterface.getNetworkInterfaces(); // 遍历所用的网络接口 while (netfaces.hasMoreEle

Oracle 使用本地IP地址连接异常的解决方法

前几天的安装的Oracle测试环境,今天发现不能使用本地IP连接,连接提示错误 "Oracle the network adapter could not establish the connection" 这个问题折腾我时间比较长,网上很多资料都不太适合我的情况.不过最后还是被找到了 花了很多时间在其他方面没有锁定问题根源. 1 开始以为是防火墙 server client端都排除 2 oracle client端问题 (ubuntu 安装的client端) 3 最后用程序测试发现还

C#之获取本地IP地址

最近协助一个项目解决了一个获取IP地址的问题,手机客户端与WebService进行通讯,然后WebService通过TCP通讯把指令传递到另一台PC机上.在测试的过程中,总是会出现WebService服务器和PC机通讯失败的问题,但是用TCP调试工具进行通讯调试这两台机器是可以进行通讯的,进行调试好了之后,换了另外的网络环境又通讯不了.最后拿到了代码,排查出来是IP地址获取的问题. 原始代码获取IP地址是通过Dns.GetHostAddresses(Dns.GetHostName())进行获取I

C#获取本地IP地址[常用代码段]

获得当前机器的IP代码,假设本地主机为单网卡 string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP string strAddr = ipEntry.AddressList[0].ToString(); //假设本地主机为单网卡 名字空间 using System.Net

获取本地ip地址

#import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve the current inte

C#获取本地IP地址

public static string GetLocalIp() { IPAddress localIp=null; try { IPAddress[] ipArray; ipArray=Dns.GetHostAddress(Dns.GetHostName()); localIp=ipArray.Frist(ip=>ip.AddressFamily==AddressFamily.InterNetwork); } catch(Exception ex) { } if(localIp==null)

Qt获取本地IP地址

1.头文件 #include<QHostAddress> #include<QNetworkInterface> 2.代码 1 QList<QHostAddress> list = QNetworkInterface::allAddresses(); 2 foreach(QHostAddress address, list) 3 { 4 //qDebug()<<address.toString(); 5 if(address.protocol() == QA