Linux/C + + to get network card IP by rule

Source: Internet
Author: User
Tags sin strcmp
Linux/C + + to get network card IP by rule

Output items to the cloud or output to external customers, will encounter the server multiple network card multiple IP situation, if there are many applications need this host IP, and multiple applications need to get the same IP, at this time can be agreed to a rule to get the same IP, such as: Get all the network card name, and then the network card name by small to large sort, Find the smallest network card name corresponding to the non-local IP, so that each application use this rule, you will get the same IP, and this idea is not limited by a variety of languages.

The detailed code is as follows:

#include <iostream> #include <stdio.h> #include <ifaddrs.h> #include <arpa/inet.h> #include

<string.h> #include <unistd.h> #include <stdlib.h> using namespace std;

int compare (const void *a,const void *b) {return strcmp (* (char * *) a,* (char * *) b);
    int Getsubnetmask (char ip[], int len) {struct sockaddr_in *sin = NULL;
    struct Ifaddrs *ifa = NULL, *iflist;
    String name[10];
    if (Getifaddrs (&iflist) < 0) {return-1;
    int i = 0; Find all network adapters and print the information for the NIC for (IFA = iflist IFA!= NULL; IFA = Ifa->ifa_next) {if (Ifa->ifa_addr->sa_fa
            Mily = = af_inet) {string ifname (ifa->ifa_name);
            Name[i]=ifname;
            i++;

            printf ("InterfaceName:%s\n", ifa->ifa_name);
            Sin = (struct sockaddr_in *) ifa->ifa_addr;

            printf ("IPAddress:%s\n", Inet_ntoa (SIN-&GT;SIN_ADDR)); Sin = (struct sockaddr_in *) IFA->ifa_dstaddr;

            printf ("Broadcast:%s\n", Inet_ntoa (SIN-&GT;SIN_ADDR));
            Sin = (struct sockaddr_in *) ifa->ifa_netmask;
        printf ("SubnetMask:%s\n\n", Inet_ntoa (SIN-&GT;SIN_ADDR)); }//Print all Non-empty network adapter name for (int i=0; i<sizeof (name)/sizeof (name[0)); i++) {if strcmp (Name[i].c_str ()
        , "")) {printf ("name%d=%s,", I,name[i].c_str ());
    printf ("\ n");
    From small to large sort qsort (name,10,sizeof (name[0]), compare);
        Find the IP for (int i=0; i<sizeof (name)/sizeof (Name[0]) of the minimum network card, i++) {if (strcmp (), "")) {for (IFA = iflist; IFA!= NULL; IFA = Ifa->ifa_next) {if (Ifa->ifa_add
                    r->sa_family = = af_inet) {string ifname (ifa->ifa_name); if (strcmp (Name[i].c_str (), ifname.c_str ()) = = 0) {printf ("Check name=%s\n", if NamE.c_str ());
                        Sin = (struct sockaddr_in *) ifa->ifa_addr;
                        Char *ipaddress = Inet_ntoa (SIN-&GT;SIN_ADDR);
                            Filter out the local if (strcmp (IPAddress, "127.0.0.1")) {
                            strncpy (Ip,ipaddress,len);
                            printf ("Last Use name=%s,ip=%s", Name[i].c_str (), IP);
                            Freeifaddrs (iflist);
                        return 0;
                        else {printf ("is");
    }}}}} Freeifaddrs (Iflist);
return-1;
    int main (int argc, char *argv[]) {char ip[64];
        if (int ret = getsubnetmask (ip,sizeof (IP)) < 0) {printf ("Find IP error\n");
    return-1;
    printf ("ip=%s\n", IP);
return 0; }

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.