zoukankan      html  css  js  c++  java
  • Linux C 获取本机IP

    Linux获取本机IP
    #include <stdio.h>
    #include 
    <sys/ioctl.h>
    #include 
    <sys/socket.h>
    #include 
    <sys/types.h>
    #include 
    <netdb.h>
    #include 
    <net/if.h>
    #include 
    <arpa/inet.h>
    #define ERRORIP "cannot find host ip"
    char *ip_search(void)
    {
        
    int sfd, intr;
        
    struct ifreq buf[16];
        
    struct ifconf ifc;
        sfd 
    = socket (AF_INET, SOCK_DGRAM, 0); 
        
    if (sfd < 0)
            
    return ERRORIP;
        ifc.ifc_len 
    = sizeof(buf);
        ifc.ifc_buf 
    = (caddr_t)buf;
        
    if (ioctl(sfd, SIOCGIFCONF, (char *)&ifc))
            
    return ERRORIP;
        intr 
    = ifc.ifc_len / sizeof(struct ifreq);
        
    while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char *)&buf[intr]));
        close(sfd);
        
    return inet_ntoa(((struct sockaddr_in*)(&buf[intr].ifr_addr))-> sin_addr);
    }
    int main(void)
    {
        printf(
    "%s\n", ip_search());
        
    return 0;
    }

     转,完全不懂啊,( > c < ) !

  • 相关阅读:
    Android开发系列之ListView用法
    自省
    fake feeling ?
    我心中的天使
    2020.6.16
    python面试题
    tcp
    Short + skip + limit
    01_pyttsx3_将文本文字转为语音播放
    mongo_Linux下不进入数据库查数据
  • 原文地址:https://www.cnblogs.com/yangyh/p/1761164.html
Copyright © 2011-2022 走看看