Three methods for obtaining the IP address of the local machine through Linux Programming

Source: Internet
Author: User
Tags get ip

This
It is a job that is not clear and meaningless. One reason is that the network address settings are flexible and allow users to customize the settings. For example, a computer can have multiple physical NICs or virtual
A network adapter can be bound to multiple IP addresses. You can set an alias for the network adapter and rename the network adapter. The network topology of your computer is unknown. The host name setting is optional and can also be used.
This information may be affected when a computer is bound to multiple host names. A separate network address is meaningless because it is disconnected from the network connection. In programming, you must consider
One option is placed in the configuration file, which is selected by the user.

Programming to get IP addresses through Google involves three ideas:
1. Use gethostname () and gethostbyname ()

# Include <stdio. h>
# Include <unistd. h>
# Include <netdb. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>

Int main (){
Char hname [128];
Struct hostent * hent;
Int I;

Gethostname (hname, sizeof (hname ));

// Hent = gethostent ();
Hent = gethostbyname (hname );

Printf ("hostname: % S/naddress list:", hent-> h_name );
For (I = 0; hent-> h_addr_list [I]; I ++ ){
Printf ("% S/T", inet_ntoa (* (struct in_addr *) (hent-> h_addr_list [I]);
}
Return 0;
}

Run:
[WHB @ jcwkyl C] $./local_ip
Hostname: jcwkyl.jlu.edu.cn
Address List: 10.60.56.90

2. By enumerating NICs, you can view man 7 netdevice through the API interface.

/*CodeFrom stackoverflow: http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-computer */
# Include <stdio. h>
# Include <sys/types. h>
# Include <ifaddrs. h>
# Include <netinet/in. h>
# Include <string. h>
# Include <ARPA/inet. h>

Int main (INT argc, const char * argv []) {
Struct ifaddrs * ifaddrstruct = NULL;
Void * tmpaddrptr = NULL;

Getifaddrs (& ifaddrstruct );

While (ifaddrstruct! = NULL ){
If (ifaddrstruct-> ifa_addr-> sa_family = af_inet) {// check it is ip4
// Is a valid ip4 address

Tmpaddrptr = & (struct sockaddr_in
*) Ifaddrstruct-> ifa_addr)-> sin_addr;
Char Addressbuffer [inet_addrstrlen];

Inet_ntop (af_inet, tmpaddrptr, Addressbuffer, inet_addrstrlen );

Printf ("% s IP address % s/n", ifaddrstruct-> ifa_name, Addressbuffer );

} Else if (ifaddrstruct-> ifa_addr-> sa_family = af_inet6) {// check it is ip6
// Is a valid ip6 address

Tmpaddrptr = & (struct sockaddr_in
*) Ifaddrstruct-> ifa_addr)-> sin_addr;
Char Addressbuffer [inet6_addrstrlen];

Inet_ntop (af_inet6, tmpaddrptr, Addressbuffer, inet6_addrstrlen );

Printf ("% s IP address % s/n", ifaddrstruct-> ifa_name, Addressbuffer );

}
Ifaddrstruct = ifaddrstruct-> ifa_next;
}
Return 0;
}

Run:
[WHB @ jcwkyl C] $./local_ip2
Lo IP address 127.0.0.1
Eth0 IP address 10.60.56.90
Eth0: 1 ip address 192.168.1.3
Lo IP Address ::
Eth0 IP Address: 2001: da8: b000: 6213: 20f: 1fff
Eth0 IP address 0: 0: fe80: 20f: 1fff

3. Open a network connection to the external server and use getsockname () to check its IP address.

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.