VC -- How to automatically obtain the local IP address?

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.