网络编程 UDP 设定MTU
MTU(Maximun Transmisson Unit):一次送信的最大size。
在程序里动态改变MTU。注意:程序运行需要root权限。
程序运行的方法:
sudo ./a.out
1,取得MTU
#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);
if(ioctl(fd, SIOCGIFMTU, &ifr) != 0){
perror(\"ioctl\");
return 1;
}
close(fd);
printf(\"Maximun Transmisson Unit:%dn\", ifr.ifr_mtu);
return 0;
}
2,改变MTU
#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);
//change Maximun Transmisson Unit to 1400
ifr.ifr_mtu = 1400;
if(ioctl(fd, SIOCSIFMTU, &ifr) != 0){
perror(\"ioctl\");
return 1;
}
close(fd);
printf(\"Maximun Transmisson Unit:%dn\", ifr.ifr_mtu);
return 0;
}
在命令行里也可以查看和改MTU(需要root权限)
改变MTU:
sudo ifconfig enp0s3 mtu 1400
查看当前的MTU:
sudo ifconfig enp0s3
继续阅读与本文标签相同的文章
上一篇 :
人工智能至少有一个好处 - 让硬件再次重要
下一篇 :
一份帮助你更好地理解深度学习的资源清单
-
基础c4d教程:简单的木质吊灯建模,小白也能学会
2026-05-18栏目: 教程
-
大族激光:智能装备LION系列光纤激光切割机发布仪式在湘隆重召开
2026-05-18栏目: 教程
-
实拍上汽首个“无人”仓库,本月正式运行
2026-05-18栏目: 教程
-
自动驾驶光车以外的硬件就7万 滴滴想让你不买车就能先坐上
2026-05-18栏目: 教程
-
滴滴迎来大整顿!1000万罚单认清现实,8万司机被开除
2026-05-18栏目: 教程
