如何获取本机在Internet上的IP地址?
如何获取本机在internet上的ip地址?用bcb实现程序代码请列出。
推荐阅读
关注~
随便问,如果在局域网内如何获得internet上的ip地址
我有个馊主意,写个asp页面放到internet,然后写代码打开asp文件,用来返回你的internet上的ip。楼主别骂我。:)
//关注其他的办法。
看看这个http://sanjianxia.myrice.com/vc/vc95.htm
呵呵,我可以获得局域网内的ip地址,不过internet上面的没有试过,呵呵,帮你up一下。
我也想知道!帮你up
twsadata wsdata;
if (wsastartup(makeword(1,1),&wsdata)!=0)
{ showhinttxt("获取本机ip地址失败!请查看网络连接是否正常!");
return ;
}
char hostname[80];
lphostent lphostent;
struct in_addr addr;
memset(&addr,0,sizeof(in_addr)); //对in_addr结构清0,以利后面填写
if (gethostname(hostname,sizeof(hostname))==socket_error)
{ showhinttxt("获取本机ip地址失败!请查看网络连接是否正常!");
return ;
}
lphostent=gethostbyname(hostname);//利用得到的主机名去获得主机结构
if (!lphostent){ showhinttxt("获取本机ip地址失败!请查看网络连接是否正常!");
return ;
}
ansistring asipaddr;
combobox6->items->clear();
for (int i=0; lphostent->h_addr_list[i]!=0; i++) //从主机地址表中得到ip地址
{ memcpy(&addr,lphostent->h_addr_list[i],sizeof(in_addr));
asipaddr= inet_ntoa(addr);
combobox6->items->add(asipaddr);
}
guangzhu
我建议直接用windows socket api 相关函数来获取主机在internet 上的ip地址;
建议使用gethostipaddress中完成楼主说的功能;
还有一个方法:
很简单
一步到位:void __fastcall tform1::button1click(tobject *sender)
{
if (powersock1->localip == "0.0.0.0")
showmessage("not connected");
else
showmessage(powersock1->localip);
}
api函数gethostbyname
hostent结构中有本机ip的列表,其中属于internet的ip需要程序判断。
這個問題好多人問過,好像都沒有比較完美的方法,有的只能探測到用modem撥號的ip,如果使用網關或代理服務器就不行了。我這有個方法是 chifengwatch(chifengwatch) 的原理。使用nmhttp控件
void __fastcall tfrmmain::button1click(tobject *sender)
{
nmhttp1->get("http://code.helpor.net/mine/ip.php");
ansistring getipfromurl = nmhttp1->body;
edit1->text=getipfromurl.substring(getipfromurl.pos("(")+2,getipfromurl.pos(")")-getipfromurl.pos("(")-3);
}
赞成楼上!
.

讨论区