char hostname[128]; hostent* hn; gethostname(hostname,128); hn = gethostbyname(hostname); IP 地址=inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]) IP_ADAPTER_INFO ipAdaInfo; ULONG ipSize=sizeof(ipAdaInfo); if(GetAdaptersInfo(&ipAdaInfo,&ipSize)==ERROR_SUCCESS){ AfxMessageBox(ipAdaInfo
IpAddressList
IpAddress
String);//本机IP 地址 } 你完全不必自己取IP,客户端接受socket 连接后可以根据 IP 包直接得到IP 地址,取法为 sockaddr_in remoteServer; accept(listenSock,&remoteServer,sizeof(remoteServer)); char* cIP=inet_ntoa(remoteServer
sin_addr);//得到该数据包源IP ring 的方法更通用,可以查别的机器的IP
如果你要写Internet 程序,就不能这样去取 IP 地址,这样取的是局域网IP,不是你在Internet 上的真IP,一定得从 IP 中取得IP 地址,即上面的 第二个方法
Socket api Client: #ifndef UDPClientH #define UDPClientH #include #include #include #include #include #include "CCEdit
h" #define WM_SOCK WM_USER+100 class TLANForm : public