如何得到本机的公网IP和局域网IP

llgzhang 2006-02-27 03:54:21
当一个机器通过局域网中的服务器上Internet时,能同时得到这两个IP吗?用什么方法?
多谢!!
...全文
1186 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
jonashu 2006-04-07
  • 打赏
  • 举报
回复
看一下QQ不就可以了吗
DrSmart 2006-03-30
  • 打赏
  • 举报
回复
呵呵, 问题好像还是没有解决阿,

看看vckbase上的那个例程exe竟然是个木马,call管理员怎么当的

帮顶
sessdu 2006-03-30
  • 打赏
  • 举报
回复
楼上的太绝了,呵呵

我也在学习呢,看中
DrSmart 2006-03-22
  • 打赏
  • 举报
回复
晕,方法比较多,不过我的方法都是比较戳,先不在这里丢人,看看楼下的
Juchiyufei 2006-03-22
  • 打赏
  • 举报
回复
学习一下.
xx_jj 2006-03-22
  • 打赏
  • 举报
回复
只能用楼上的办法得到
要不只是本机网络邻居射的那个
billy145533 2006-03-22
  • 打赏
  • 举报
回复
通过 ip 识别的网站
构建请求包,然后等待返回的数据
chehw 2006-03-22
  • 打赏
  • 举报
回复
给自己家里的电脑发个包, 家里电脑获取你的公网ip后再传给你

(如果你的程序不能直接处理网关路由器或计算机, 大概只能如此了)
N_thread 2006-03-22
  • 打赏
  • 举报
回复
Network Address Translation Traversal Interfaces
The following interfaces make it possible to manage Network Address Translation (NAT) through UPnP™.

Interface Purpose
IUPnPNAT Primary interface for UPnP management of NAT.
INATEventManager Provides methods to register application-defined callback interfaces.
IDynamicPortMappingCollection This interface is not currently supported.
IDynamicPortMapping This interface is not currently supported.
IStaticPortMappingCollection Collection interface for static port mappings.
IStaticPortMapping Provides methods for managing a specific port mapping.


The following interfaces are implemented by the client application. The NAT calls the methods in these interfaces in order to inform the client of changes in the NAT configuration.

Interface Purpose
INATExternalIPAddressCallback Provides a method that the system calls if the NAT's external IP address changes.
INATNumberOfEntriesCallback Provides a method that the system calls if the number of port mappings changes.
zblaoshu1979 2006-03-22
  • 打赏
  • 举报
回复
IPCONFIG
wangwenjuan 2006-03-22
  • 打赏
  • 举报
回复
这个问题我也找了两天了,没有得到很好解决,也等楼下的吧
localman 2006-03-07
  • 打赏
  • 举报
回复
获得公网ip好像只能服务器返回吧
bimd 2006-03-07
  • 打赏
  • 举报
回复
我也在被这个问题困扰,我现在通过局域网代理上网,qq能够识别出我所在地市,这个是如何做到的.
llgzhang 2006-03-01
  • 打赏
  • 举报
回复
TO:striking(硬撑者) ( ) 信誉:98
我的环境是通过局域网上的internet,我用了你的代码,得到的IP是局域网的IP。你说的“如果你在局域网就是局域网的ip , 在公网就是公网的ip”公网是指通过modem直接上网的情况吧
alfwolf 2006-02-28
  • 打赏
  • 举报
回复
2楼的方法只可以得到系统所设置的IP,如果阁下的机器是通过NAT接入互联网,那么这种方法就有问题.
仅仅当外部服务器能够在数据包中返回你的NAT信息时才可以.如果两端都运行阁下的程序进行P2P连接,那就好办了.
fengge8ylf 2006-02-28
  • 打赏
  • 举报
回复
WWW.VCKBASE.COM 上有相关程序
Hylas 2006-02-28
  • 打赏
  • 举报
回复
这个问题不是前阵有人问过吗。。。
「已注销」 2006-02-28
  • 打赏
  • 举报
回复
当然可以,在局域网的机器要获知他的外网IP,通过STUN协议即可,另外,如果网关设备支持UPNP技术的话,也可以通过UPNP技术,发送GetExternalIP指令(通过构造数据包)来获取网关的外网地址,最通用的是通过STUN协议来获取,他是通过服务器返回消息得知自己的外网IP的

STUN协议可以参考:http://sourceforge.net/projects/stun/
striking 2006-02-27
  • 打赏
  • 举报
回复
通过socket api可以得到ip, 如果你在局域网就是局域网的ip , 在公网就是公网的ip
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData)) {
printf("WSAStartup failed %s\n", WSAGetLastError());
return -1;
}

//////////////////
// Get host name.
//
char hostname[256];
int res = gethostname(hostname, sizeof(hostname));
if (res != 0) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
printf("hostname=%s\n", hostname);

////////////////
// Get host info for hostname.
//
hostent* pHostent = gethostbyname(hostname);
if (pHostent==NULL) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}

//////////////////
// Parse the hostent information returned
//
hostent& he = *pHostent;
printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
he.h_name, he.h_aliases, he.h_addrtype, he.h_length);

sockaddr_in sa;
for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
// Output the machines IP Address.
printf("Address: %s\n", inet_ntoa(sa.sin_addr)); // display as string
}

//////////////////
// Terminate windows sockets API
//
WSACleanup();
oyljerry 2006-02-27
  • 打赏
  • 举报
回复
内网IP,可以通过iphelp系列函数等得到~
外网的,需要能记录IP的网站等返回结果,不好得到

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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