从许多教程中,我得到了以下知识(也许我被误解了):
一个以太网数据包的最大长度约为 1500 字节。
IP 数据包的最大长度约为 65535 字节。
UDP 数据包的最大长度为 65515 字节
但是当我进行测试并观看 Wireshark 时,我得到了不同的答案。
- 我尝试使用 TCP 协议发送一些大数据。
Socket con = new Socket("localhost", 8088);
OutputStream os = con.getOutputStream();
StringBuilder s = new StringBuilder();
for( int i = 0; i < 10000; i++) {
s.append("Hello world");
}
// about 110k bytes
byte[] data = s.toString().getBytes();
os.write(data);
os.close();
con.close();
这是我的 Java 代码(没有必要理解这一点。),我尝试使用 TCP 连接发送 110k 字节的数据。这是我的 Wireshark。
我的 110k 字节消息被拆分为 7 个数据包,我认为这表明 TCP 数据包的最大长度为 16388 字节。
- 然后,我尝试发送一个 UDP 数据包:
DatagramSocket client = new DatagramSocket(50555);
StringBuilder s = new StringBuilder();
for( int i = 0; i < 10000; i++) {
s.append("Hello world");
}
// 110k bytes
byte[] data = s.toString().getBytes();
int messageLength = data.length;
for (; ; messageLength--){
try{
DatagramPacket packet = new DatagramPacket(data, messageLength,
new InetSocketAddress("localhost", 8088));
// If packet is still too lang, above line will throws an exception
// If there is not any exception, means we can send this packet
// and this messageLength is the limit value for a UDP packet.
client.send(packet);
System.out.println("message length is " + messageLength);
// break for loop
break;
} catch(Exception e){
// fail to send and continue for loop
}
}
client.close();
结果是message length is 65507
。
我真的很困惑:
IP协议建立在以太网或其他东西上,当以太网只能发送1500字节时,为什么IP数据包可以是65535字节?
为什么一个 TCP 数据包只有 16388 字节?
然后我在 SOF 或其他网站上阅读了很多帖子,但我没有得到答案,我认为不会与其他人重复。