The problem is very clear, and the code is directly pasted.
# Include "iostream" # include "winsock2.h" # pragma comment (Lib, "ws2_32.lib") using namespace STD; char * fetchipaddress () {char host_name [225]; if (gethostname (host_name, sizeof (host_name) = socket_error) {cout <"error" <wsagetlasterror () <"when getting local host name. "<Endl;} struct hostent * Phe = gethostbyname (host_name); If (PHE = 0) {cout <" yow! Bad host lookup. ";}struct in_addr ADDR; memcpy (& ADDR, Phe-> h_addr_list [0], sizeof (struct in_addr); Return inet_ntoa (ADDR);} Char * getip () {wsadata; If (wsastartup (makeword (1,1), & wsadata )! = 0) {return NULL;} Char * IPaddress = fetchipaddress (); wsacleanup (); Return IPaddress;} int main () {char * IP = getip (); cout <IP <Endl; return 0 ;}
When obtaining the IP address, refer to the calling method in the main function.