獲取本地IP

2021-05-22 04:42:46 字數 432 閱讀 4773

bool socketuti::getlocalipaddr(string &stripaddr)

if(gethostname(myname,80)==socket_error)

return false;

if( !(thishost=gethostbyname(myname)) )

return false;

memset((void *)&in,sizeof(in),0);

in.s_addr=*((unsigned long *)thishost->h_addr_list[0]);

if( !(ptr=inet_ntoa(in)) )

return false;

wsacleanup();

stripaddr=string(ptr);

return true;

}

獲取本地IP

本機可能會有多個ip,下面這段程式可以獲得本機ip struct hostent h char host 100 int i gethostname host,100 if h gethostbyname host null printf error s n hstrerror h errno re...

獲取本地IP

方法一 foreach ipaddress ip in dns.gethostentry dns.gethostname addresslist 方法二 iphostentry ipentry dns.gethostbyname dns.gethostname gethostbyname 方法已過時...

vc獲取本地IP

include include pragma comment lib,ws2 32.lib void getlocalip void for int i 0 phe h addr list i 0 i wsacleanup int main void wsadata wsddata 這個結構被用來儲...