您当前的位置: 首页 >  c++

phymat.nico

暂无认证

  • 3浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c++ socket启动网卡接口

phymat.nico 发布时间:2020-07-20 15:47:43 ,浏览量:3

//启动网卡接口 
int SetEtherStatusUP(const char *ethNum)
{
struct ifreq ifr;
int sockfd;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("Create socket fails!\n");
return -1;
}

strcpy(ifr.ifr_name, ethNum);
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
{
printf("ioctl SIOCGIFFLAGS fails!\n");
close(sockfd);
return -1;
}

ifr.ifr_flags |= IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
{
printf("ioctl SIOCSIFFLAGS fails!\n");
close(sockfd);
return -1;
}

close(sockfd);

return 1;
}

//关闭网卡接口 
int SetEtherStatusDOWN(const char *ethNum)
{
struct ifreq ifr;
int sockfd;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("Create socket fails!\n");
return -1;
}

strcpy(ifr.ifr_name, ethNum);
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
{
printf("ioctl SIOCGIFFLAGS fails!\n");
close(sockfd);
return -1;
}

ifr.ifr_flags &= ~IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0)
{
printf("ioctl SIOCSIFFLAGS fails!\n");
close(sockfd);
return -1;
}

close(sockfd);

return 1;
}

 

关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.0449s