獲取本機所有ip位址
#include
#include
#include
#include
#pragma comment (lib, "ws2_32.lib")
void
get_host_ip()
int ret;
struct addrinfo hints;
struct addrinfo *res,
*cur;
struct sockaddr_in *addr;
char m_ipaddr[16]
;memset
(&hints,0,
sizeof
(struct addrinfo));
hints.ai_family = af_inet;
/* allow ipv4 */
hints.ai_flags = ai_passive;
/* for wildcard ip address */
hints.ai_protocol =0;
/* any protocol */
hints.ai_socktype = sock_dgram;
ret =
getaddrinfo(""
,null
,&hints,
&res);if
(ret ==-1
)for
(cur = res; cur !=
null
; cur = cur->ai_next)
freeaddrinfo
(res)
;wsacleanup()
;}intmain
(int argc,
char
**ar**)
vc++獲取本機位址
//m_sock為csocket類物件
initinfo()
//獲取裝置名稱
sinfo& info = m_info;
gethostname
(info.shost,
sizeof
(info.shost));
//獲取本機ip位址
addrinfoa hints;
addrinfoa* result;
zeromemory
(&hints,
sizeof
(addrinfo));
hints.ai_family = af_inet;
hints.ai_protocol =0;
hints.ai_flags = ai_passive;
hints.ai_socktype = sock_dgram;
int res =
getaddrinfo
(info.shost,
null
,&hints,
&result)
; sockaddr_in* addr =
(sockaddr_in*
)result-
>ai_addr;
in_addr* ad =
(in_addr*
)&addr-
>sin_addr;
inet_ntop
(result-
>ai_family, ad, info.sip,
sizeof
(info.sip));
//獲取使用者名稱
dword dw =
sizeof
(info.sname)
;getusername
(info.sname,
&dw)
;//以255結尾的ip位址是虛擬ip,用於網段內**資料
cstring sip
(info.sip)
;int i = sip.
reversefind
('.');
sip = sip.
left
(i +1)
+_t("255");
spack pack =
;memcpy
(pack.data,
&info,
sizeof
(info));
m_sock.
sendto
(&pack,
sizeof
(info)
+sizeof
(int
), uport, sip)
;}
獲取本機IP位址
cstring strlocalip char szhostname 64 memset szhostname,0x00,sizeof szhostname gethostname szhostname,sizeof szhostname phostent phost gethostbyname s...
獲取本機IP位址
第一步 載入winsock2靜態鏈結庫檔案。include pragma comment lib,ws2 32.lib bool loadwinsock 第二步 獲取本地的主機名稱。hostent phostent null char szhostname 256 memset szhostname...
獲取本機IP位址
獲取本機ip string hostname dns.gethostname iphostentry ipentry dns.gethostentry hostname 得到主機ip ipaddress addresslist ipentry.addresslist 得到本機所有的ip位址strin...