当前位置: 首页 > news >正文

VC++ 设置网卡接口MTU大小

在 Windows C/C++ 之中一共有三种方法可以设置网卡的MTU大小。

方法一:

SetIpInterfaceEntry 法

            static bool SetInterfaceMtu2(int interface_index, int mtu) noexcept{PIP_ADAPTER_ADDRESSES pAddresses = NULL;ULONG ulBufLen = 0;GetAdaptersAddresses(AF_UNSPEC, 0, NULL, pAddresses, &ulBufLen);char* szBuf = (char*)Malloc(ulBufLen);pAddresses = (IP_ADAPTER_ADDRESSES*)szBuf;if (NULL == pAddresses){return false;}NETIOAPI_API dwErr = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST, NULL, pAddresses, &ulBufLen);if (dwErr == NO_ERROR){while (NULL != pAddresses){if (pAddresses->IfIndex == interface_index) {MIB_IPINTERFACE_ROW ifRow;InitializeIpInterfaceEntry(&ifRow);//interested nameifRow.InterfaceLuid = pAddresses->Luid;ifRow.Family = AF_INET;ifRow.NlMtu = mtu;dwErr = SetIpInterfaceEntry(&ifRow);break;}pAddresses = pAddresses->Next;}}Mfree(szBuf);return dwErr == NO_ERROR;}

方法二:

SetIfEntry 法

            std::shared_ptr<MIB_IFROW> GetIfEntry(int interface_index) noexcept{if (interface_index < 0){return NULL;}std::shared_ptr<MIB_IFROW> pIfRow = std::shared_ptr<MIB_IFROW>((MIB_IFROW*)Malloc(sizeof(MIB_IFROW)),[](MIB_IFROW* p) noexcept{Mfree(p);});if (NULL == pIfRow){return NULL;}pIfRow->dwIndex = interface_index;if (::GetIfEntry(pIfRow.get()) != NO_ERROR){return NULL;}return pIfRow;}int GetInterfaceMtu(int interface_index) noexcept{std::shared_ptr<MIB_IFROW> ifRow = GetIfEntry(interface_index);if (NULL == ifRow){return -1;}else{return ifRow->dwMtu;}}bool SetInterfaceMtu(int interface_index, int mtu) noexcept{// https://learn.microsoft.com/en-us/windows/win32/api/netioapi/nf-netioapi-getifentry2// https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getifentrystd::shared_ptr<MIB_IFROW> ifRow = GetIfEntry(interface_index);if (NULL == ifRow){return false;}if (ifRow->dwMtu == mtu){return true;}else{ifRow->dwMtu = mtu;}DWORD dwErr = SetIfEntry(ifRow.get());if (dwErr == NO_ERROR){if (GetInterfaceMtu(interface_index) == mtu){return true;}}return SetInterfaceMtu2(interface_index, mtu);}

方法三:

命令行法

查看网络接口设置(MTU列项)

netsh interface ipv4 show subinterfaces

通过网络接口索引来管理,MTU是大小,10为网络接口索引

 netsh interface ipv4 set subinterface 10 mtu=1400 store=persistent

通过网络接口名字来管理,MTU是大小,eth0为网络接口名

netsh interface ipv4 set subinterface eth0 mtu=1400 store=persistent

http://www.lryc.cn/news/318605.html

相关文章:

  • dpdk-19.11 对向量指令的使用情况分析
  • 使用CIP采集欧姆龙EtherNet/IP从入门到精通
  • 企业如何高效管理微信里的客户?
  • 怎么在windows系统上查看kylinos的md5、sha1、sha256值
  • Windows中在C#中使用Dapper和Mysql.Data库连接MySQL数据库
  • 大一专科,物联网专业,变态成长偏方!
  • MyBatis入门(JDBC规范,MyBatis,连接池,Lombok)【详解】
  • Vue3--数据和方法
  • 网络编程面试题
  • 移动端区分点击和长按
  • 虚拟环境的激活
  • 宏集案例 | 风电滑动轴承齿轮箱内多点温度采集与处理
  • linux 16进制写入
  • 代码随想录算法训练营第60天| Leetcode 84.柱状图中最大的矩形
  • 编写一个简单的cmakelist.txt
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的零售柜商品检测软件(Python+PySide6界面+训练代码)
  • 数据库的学习
  • matlab去除图片上的噪声
  • C++超详细知识点(五):类的友元函数和友元类
  • SOC设计:关于reset的细节
  • 支小蜜AI校园防欺凌系统可以使用在宿舍吗?
  • 外卖平台订餐流程架构的实践
  • [AIGC] Spring Boot中的切面编程和实例演示
  • 各个类型和Json类型的相互转换
  • C语言:操作符详解(下)
  • 电商场景下 ES 搜索引擎的稳定性治理实践
  • jdk8与jdk17的区别。springboot2.x与springboot3.x的区别
  • Pytest测试中的临时目录与文件管理!
  • arduino 编程esp8266
  • 基于springboot实现数据资产管理系统项目【项目源码+论文说明】计算机毕业设计