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

时间:2023-01-01 20:04:57

网络编程 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;
}

github源代码

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; }

github源代码

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

down网卡:

sudo ifconfig enp0s3 down

up网卡:

sudo ifconfig enp0s3 up

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

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

本人微信:xiaoshitou5854

c/c++ 网络编程 UDP up/down 网卡的更多相关文章

  1. c&sol;c&plus;&plus; 网络编程 UDP 改变网关和网卡名字

    网络编程 UDP 改变网关和网卡名字 在程序里动态改变网关和网卡名字 1,改变网卡名字 #include <stdio.h> #include <string.h> #incl ...

  2. c&sol;c&plus;&plus; 网络编程 UDP 改变网卡的硬件地址

    网络编程 UDP 改变网卡的硬件地址 在程序里动态改变网卡的硬件地址 1,取得网卡的硬件地址 #include <stdio.h> #include <string.h> #i ...

  3. c&sol;c&plus;&plus; 网络编程 UDP 用if&lowbar;nameindex和ioctl取得主机网络信息

    网络编程 UDP 用if_nameindex和ioctl取得主机网络信息 getifaddrs函数取得的东西太多了,如果只想取得网卡名字和网卡编号可以用下面的2个函数. 1,if_nameindex ...

  4. c&sol;c&plus;&plus; 网络编程 UDP 主机网络信息取得

    网络编程 UDP 主机网络信息取得 1,if_nametoindex 通过网卡名字取得网卡编号 2,if_indextoname 通过网卡编号取得网卡名字 #include <stdio.h&g ...

  5. c&sol;c&plus;&plus; 网络编程 UDP 设定MTU

    网络编程 UDP 设定MTU MTU(Maximun Transmisson Unit):一次送信的最大size. 在程序里动态改变MTU.注意:程序运行需要root权限. 程序运行的方法: sudo ...

  6. c&sol;c&plus;&plus; 网络编程 UDP 改变IP地址

    网络编程 UDP 改变IP地址 在程序里动态改变主机的IP地址 1,改变ipv4的地址 #include <stdio.h> #include <string.h> #incl ...

  7. c&sol;c&plus;&plus; 网络编程 UDP 发送端 bind 作用

    网络编程 UDP 发送端 bind 作用 upd 发送端 调用bind函数的效果:把socket特定到一个指定的端口,如果不调用bind,内核会随机分配一个端口. upd 发送端 调用bind函数的目 ...

  8. Socket网络编程-UDP编程

    Socket网络编程-UDP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.UDP编程概述 1>.UDP服务端编程流程 创建socket对象.socket.SOCK_ ...

  9. java 网络编程 UDP TCP

    网络编程 网络编程主要用于解决计算机与计算机(手机.平板..)之间的数据传输问题. 网络编程: 不需要基于html页面就可以达到数据之间的传输. 比如: feiQ , QQ , 微信....网页编程: ...

随机推荐

  1. Android M&lpar;6&period;0&rpar; 权限相关

    原文链接:http://jijiaxin89.com/2015/08/30/Android-s-Runtime-Permission/ Android M 新的运行时权限开发者需要知道的一切   an ...

  2. Web&period;xml各版本模版

    web.xml v2.3 web.xml v2.4 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  3. js用8421码实现10进制转2进制

    今天早上突然心血来潮决定用 ''和js来撸一个进制转换.(纯属心血来潮,有兴趣的可以看看.) 我们知道,通过8421码.可以快速的得到一个10进制的2进制.如下图: 如上图所示:我们将10进制的 '1 ...

  4. copy&lowbar;to&lowbar;user&comma;copy&lowbar;from&lowbar;user&comma;get&lowbar;user&comma;put&lowbar;user函数比较

    copy_to_user,copy_from_user,get_user,put_user函数比较 copy_to_user --  Copy a block of data into user sp ...

  5. 利用php数组函数进行函数式编程

    因为一个BUG, 我在一个摇摇欲坠,几乎碰一下就会散架的项目中某一个角落中发现下面这样一段代码 这段程序与那个BUG有密切的关系. 我来回反复的捉摸这段代码, 发现这段代码实现了两个功能 第一个是在一 ...

  6. &lbrack;Leetcode 72&rsqb;编辑距离 Edit Distance

    [题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...

  7. 万恶之源 - Python文件操作

    文件操作 初始文件操作 使用Python来读写文件是非常简单的操作,我们使用open()函数来打开一个文件,获取到文件句柄,然后通过文件句柄就可以进行各种各样的操作了 根据打开方式的不同能够执行的操作 ...

  8. Grasshopper操作shp

    1 shp文件组件 提示ACE.OLEDB  未注册. 需要安装acess控件,https://www.microsoft.com/zh-CN/download/details.aspx?id=132 ...

  9. 20155232 2016-2017-3 《Java程序设计》第7周学习总结

    20155232 2016-2017-3 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 1.Greenwich MeanTime,格林威治时间,简称GMT时间,由观察太阳 ...

  10. &lbrack;Delphi&rsqb; 调用ocx

    function RegisterDllServer(FileName: string): boolean; var nDllAddr: integer; bstr: string; ProcAddr ...