在win7系统下获取的客户端IP是16位进制数

todayclock 2010-09-28 03:00:12
能获取到像“192.168.1.1”的IP值吗?或者还是需要转换之类的方法。
下面是我获取IP的方法

/// <summary>
/// 获取本机IP
/// </summary>
/// <returns></returns>
private string GetIP()
{
string hostName = Dns.GetHostName(); //得到主机名
IPHostEntry ipEntry = Dns.GetHostEntry(hostName);//得到主机IP
string ip = ipEntry.AddressList[0].ToString();
return ip;
}

我把host文件改成
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# 192.168.1.110 localhost


127.0.0.1 www.template.com
127.0.0.1 www.youxi.com
红色是我修改的部分, 改完之后获取的IP是::1(都是localhost的标识)
这个具体要怎么改?
...全文
342 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovetrue 2011-05-16
  • 打赏
  • 举报
回复
楼上的还是16的原因是,8楼的代码有个小错误,既判断IP地址地方有误还有就是他的判断有误因为他获得IP后没有跳出循环,这样判断就无意义了
tkhhappyboy1 2011-03-07
  • 打赏
  • 举报
回复
怎么还是16进制的呢?
CLove2010 2010-11-22
  • 打赏
  • 举报
回复
哥们把你判断的代码给我看一下,具体是怎么做的,谢谢!
todayclock 2010-11-22
  • 打赏
  • 举报
回复
现在不用这个方法了,它一直获取的是服务器IP,对于各个用户访问的网站的客户端IP是获取不到的。
具体判断:

public static string GetIP()
{
string hostName = Dns.GetHostName(); //得到主机名
IPHostEntry ipEntry = Dns.GetHostEntry(hostName);//得到主机IP
string ip = "";
foreach (IPAddress i in ipEntry.AddressList)
{
if (i.ToString().Split('.')[0] != "")
{
ip = i.ToString();
}
else
{
ip = "127.0.0.1";
}
}
return ip;
}

todayclock 2010-09-28
  • 打赏
  • 举报
回复
知道了。
ipEntry.AddressList 获取的列表值中有10进制数IP,
判断一下,获取到了。
谢谢了。
vrhero 2010-09-28
  • 打赏
  • 举报
回复
host文件仅仅是本地dns,你还要设置网卡才行...
todayclock 2010-09-28
  • 打赏
  • 举报
回复
vrhero
(若批评不自由则赞美无意义...)

那个设置是无效的,不知道怎么回事,我把它修改之后,获取的值就不一样。
todayclock 2010-09-28
  • 打赏
  • 举报
回复
同样的设置我在XP,2003中都是10进制数的IP。
vrhero 2010-09-28
  • 打赏
  • 举报
回复
再ps:你不把192.168.1.110前面那个“#”删掉怎么会有这个地址...
vrhero 2010-09-28
  • 打赏
  • 举报
回复
foreach(... in ipEntry.AddressList)

ps:跟win不win7没关系...

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧