c/c++语言开发共享c/c++ 网络编程 UDP up/down 网卡

网络编程 UDP up/down 网卡 在程序里动态改变网卡的状态。注意:程序运行需要root权限。 程序运行的方法: 1,关闭网卡 “github源代码” 2,打开网卡 “github源代码” 在命令行里也可以down和up网卡(需要root权限) down网卡: up网卡: c/c++ 学习互助 …


网络编程 udp up/down 网卡

在程序里动态改变网卡的状态。注意:程序运行需要root权限。

程序运行的方法:

sudo ./a.out

1,关闭网卡

#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h>  int main(){   int fd;   ifreq ifr;    fd = socket(af_inet, sock_dgram, 0);    strncpy(ifr.ifr_name, "enp0s3", ifnamsiz - 1);    //get current status   if(ioctl(fd, siocgifflags, &ifr) != 0){     perror("ioctl");     return 1;   }    //let net work down   ifr.ifr_flags &= ~iff_up;   //change status   if(ioctl(fd, siocsifflags, &ifr) != 0){     perror("ioctl");     return 1;   }   close(fd);   return 0; } 

2,打开网卡

#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h>  int main(){   int fd;   ifreq ifr;    fd = socket(af_inet, sock_dgram, 0);    strncpy(ifr.ifr_name, "enp0s3", ifnamsiz - 1);    //get current status   if(ioctl(fd, siocgifflags, &ifr) != 0){     perror("ioctl");     return 1;   }    //let net work up   ifr.ifr_flags |= iff_up | iff_running;   //change status   if(ioctl(fd, siocsifflags, &ifr) != 0){     perror("ioctl");     return 1;   }   close(fd);   return 0;  } 

在命令行里也可以down和up网卡(需要root权限)

down网卡:

sudo ifconfig enp0s3 down

up网卡:

sudo ifconfig enp0s3 up

c/c++ 学习互助qq群:877684253

c/c++  网络编程 UDP up/down 网卡

本人微信:xiaoshitou5854

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/606931.html

(0)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐