linuxc获取IP地址的命令代码

发布时间:2016-11-12 16:32

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char **argv)

{

int sockfd;

struct ifreq ifr;

char *ifname="eth0";

strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(ioctl(sockfd, SIOCGIFADDR, &ifr) == -1)

{

printf("Errorn");

return;

}

struct sockaddr_in *myaddr;

myaddr = (struct sockaddr_in*)&(ifr.ifr_addr);

printf("IP: %sn", inet_ntoa(myaddr->sin_addr));

}

linuxc获取IP地址的命令代码的评论条评论