/* Compiling environment: Visual C ++ */
- # Include <stdio. h>
- # Include <winsock2.h>
- # Pragma comment (Lib, "ws2_32.lib ")
- Int doit (INT, char **)
- {
- Char host_name [2, 255];
- // Obtain the local host name
- If (gethostname (host_name, sizeof (host_name) = socket_error ){
- Printf ("error % d when getting local host name./N", wsagetlasterror ());
- Return 1;
- }
- Printf ("host name is: % s/n", host_name );
- // Obtain the corresponding "host" from the host name database"
- Struct hostent * Phe = gethostbyname (host_name );
- If (PHE = 0 ){
- Printf ("yow! Bad host lookup .");
- Return 1;
- }
- // Cyclically obtain all IP addresses of the Local Machine
- For (INT I = 0; phe-> h_addr_list [I]! = 0; ++ I ){
- Struct in_addr ADDR;
- Memcpy (& ADDR, Phe-> h_addr_list [I], sizeof (struct in_addr ));
- Printf ("address % d: % s/n", I, inet_ntoa (ADDR ));
- }
- Return 0;
- }
- Int main (INT argc, char * argv [])
- {
- Wsadata;
- If (wsastartup (makeword (1, 1), & wsadata )! = 0 ){
- Return 255;
- }
- Int retval = doit (argc, argv );
- Wsacleanup ();
- Return retval;
- }