1、查看本地MTU值及设置MTU值
netsh interface ipv4 set subinterface "本地连接" mtu=1460 store=persistent
netsh interface ipv4 show subinterfaces
2、在读取socket中流时,如果用
读取socket中流,由于read方法会阻塞,用-1判断流结束,会出现半包问题
//此种方法读取出现不完整包情况
while ((size = reader.read(bytes)) != -1) {
str += new String(bytes, 0, size);
break;
}
//通过包头发来长度,按长度读取报文内容。
// 读取前11位,获取报文真实长度
byte[] by11 = new byte[11];
reader.read(by11, 0, 11);
String str11 = new String(by11);
log.info("读取socket报文前11位:" + str11);
int tLen = getRealLen(by11);
// 根据真实长度,读取完整报文
byte[] by22 = new byte[tLen - 11];
reader.read(by22, 0, tLen - 11);
String str22 = new String(by22);
log.info("读取socket报文后面" + (tLen - 11) + "位:" + str22);
System.arraycopy(by11, 0, bytes, 0, by11.length);
System.arraycopy(by22, 0, bytes, by11.length, by22.length);
str = new String(bytes, 0, tLen);
3、Wireshark抓包,安装比较坑,记得勾选必要插件,否则没法抓包。
设置抓包过滤IP: ip.src==192.1.1.1 and data.len>50
参考:
https://www.cnblogs.com/staff/p/9643682.html
https://my.oschina.net/u/3318187/blog/1635082
https://www.cnblogs.com/yxh168/articles/9033311.html
本文详细介绍如何查看和设置本地MTU值,解决Socket编程中流读取的半包问题,以及使用Wireshark进行有效的网络抓包。涵盖MTU调整、Socket流正确读取策略和Wireshark抓包配置,为网络编程提供实用指导。

3593

被折叠的 条评论
为什么被折叠?



