gethostaddr
#include <stdio.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <string.h>

int main(void)
{
    int                fd; 
    char               buf[100];
    struct ifreq       ifr;
    struct sockaddr_in *sa = NULL;

    fd = socket(AF_INET, SOCK_STREAM, 0); 
    strcpy(ifr.ifr_name, "eth0");
    if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
        perror("ioctl");
    }   
    sa = (struct sockaddr_in *)(&ifr.ifr_addr);
    printf("ip: %s\n", inet_ntop(AF_INET, &sa->sin_addr, buf, sizeof (buf)));
    return 0;
}

相关文章:

  • 2021-11-16
  • 2021-12-02
  • 2022-02-19
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-03-08
猜你喜欢
  • 2022-12-23
  • 2021-08-09
  • 2022-12-23
  • 2021-11-02
  • 2021-08-30
  • 2022-12-23
  • 2021-09-10
相关资源
相似解决方案