当前位置: 技术问答>linux和unix
想要使用ioctl函数设置ip地址,出现点问题求大神帮忙解决下!
来源: 互联网 发布时间:2017-05-21
本文导语: struct sockaddr_in sin; struct ifreq ifr; int fd; char *name="eth1",*address="192.168.1.175"; bzero(&ifr,sizeof(struct ifreq)); fd=socket(AF_INET,SOCK_DGRAM,0); strncpy(ifr.ifr_name,name,IFNAMSIZ); ifr.ifr_name[I...
struct sockaddr_in sin;
struct ifreq ifr;
int fd;
char *name="eth1",*address="192.168.1.175";
bzero(&ifr,sizeof(struct ifreq));
fd=socket(AF_INET,SOCK_DGRAM,0);
strncpy(ifr.ifr_name,name,IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin,0,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr(address);
memcpy(&ifr.ifr_addr,&sin,sizeof(sin));
if(ioctl(fd,SIOCSIFADDR,&ifr)