1
# 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)
{
System ("pause ");
Return 255;
}
Int retval = doit (argc, argv );
Wsacleanup ();
System ("pause ");
Return retval;
}
2
// # Include <winsock2.h>
// Connect ws2_32.lib
Void ccwebclientdlg: initsocket ()
{
Word wversionrequested;
Wsadata;
Char name [255];
Cstring IP;
Int err; phostent hostinfo;
Wversionrequested = makeword (1, 1); // version 1.1
// 1. Load the socket Library
Err = wsastartup (wversionrequested, & wsadata );
If (Err! = 0)
{
Return;
} // Determine whether the winsocket version we requested is correct. If not
// Call wsacleanup to terminate the use of winsocket and return if (lobyte (wsadata. wversion )! = 1 |
Hibyte (wsadata. wversion )! = 1)
{
Wsacleanup ();
Return;
} //... If (ERR = 0)
{
If (gethostname (name, sizeof (name) = 0)
{
If (hostinfo = gethostbyname (name ))! = NULL)
{
IP = inet_ntoa (* (struct in_addr *) * hostinfo-> h_addr_list );
}
}
Wsacleanup ();
}
Setdlgitemtext (idc_edit_port, name); // The local name is in the name
Setdlgitemtext (idc_ipaddress1, ip); // the IP address is the local IP address.
}
3
Need to include: # include <iphlpapi. h>
Add: iphlpapi. Lib
DWORD dwresult;
Ulong outbuflen;
Ip_adapter_info adapterinfo;
Outbuflen = sizeof (ip_adapter_info );
Dwresult = getadaptersinfo (& adapterinfo, & outbuflen );
// NIC address: adapterinfo. adaptername // ip: adapterinfo. ipaddresslist. IPaddress // subnet mask: adapterinfo. ipaddresslist. ipmask fixed_info; outbuflen = sizeof (fixed_info); getnetworkparams (& fixed_info, & outbuflen); // The computer name fixed_info.hostname/DNS response