Lazarus: How to list all the available network connection on a system?

前端 未结 2 1759
孤城傲影
孤城傲影 2021-01-15 00:43

I am writing a program on a Linux system using Lazarus IDE. The program is supposed to connect to the Internet or Intranet. So, I want to display to the user list of all the

2条回答
  •  灰色年华
    2021-01-15 01:04

    The following code does work on my Linux system. It outputs all the available connection point through which you can connect to the Internet or intranet. I modified the code to print out its name and ip address.

    #include 
    #include 
    #include 
    #include 
    // you may need to include other headers
    
    int main()
    {
       struct ifaddrs* interfaces = NULL;
       struct ifaddrs* temp_addr = NULL;
       int success;
       char *name;
       char *address;
    
    
      // retrieve the current interfaces - returns 0 on success
      success = getifaddrs(&interfaces);
      if (success == 0)
      {
         // Loop through linked list of interfaces
         temp_addr = interfaces;
         while (temp_addr != NULL)
         {
            if (temp_addr->ifa_addr->sa_family == AF_INET) // internetwork only
            {
                name = temp_addr->ifa_name;
                address = inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr);
            printf("%s %s\n",name,address);
            }
    
            temp_addr = temp_addr->ifa_next;
         }
      }
    
      // Free memory
      freeifaddrs(interfaces);
    }
    

提交回复
热议问题