Ip – Why maximum length of IP, TCP, UDP packet is not suit

ethernetipmtutcpudp

From many tutorials, I get follow knowledge(maybe I'm misunderstood) :

  • maximum of an Ethernet packet is about 1500 bytes.

  • maximum of an IP packet is about 65535 bytes.

  • maximum of a UDP packet is 65515 bytes

But when I made a test and watch Wireshark, I get a different answer.

  1. I try to send some big data with TCP protocol.

    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();

This is my Java code (it's not necessary to understand this.), I try to send 110k bytes data with a TCP connection. This is my Wireshark.

enter image description here

My 110k bytes message is split to 7 packets, I think this shows that maximum length of a TCP packet is 16388 bytes.

  1. Then, I try to send a UDP packet:
    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();

the result ismessage length is 65507.

I was really confused:

  • IP protocol build on Ethernet or something, Why an IP packet can be 65535 bytes when Ethernet can only send 1500 bytes?

  • Why an TCP packet is only 16388 bytes?

Then I have read many post on SOF or other websites, But I don't get an answer, I think is not duplicate to others.

Best Answer

IP protocol build on Ethernet or something, Why an IP packet can be 65535 bytes when Ethernet can only send 1500 bytes?

Ethernet is one of several physical layers which can be used to to transport IP (and also protocols besides IP). The size of the packet a physical layer can transport is specific to this physical layer, other physical layers have other properties. Note that in your specific case of communicating on localhost (127.0.0.1) no physical layer (and thus no ethernet) is involved at all since localhost is just a logical but not a physical network interface.

The 65535 bytes limit in IP is because the length field in the IP header is only 16 bit.

Why an TCP packet is only 16388 bytes?

The applications sends data to the OS kernel which then packetizes the data. The size of the data which can be send to the kernel at once depend on the size of the socket buffer which results in the packet size you see. Additionally the kernel might further split the data to best fit the maximum size of the underlying physical layer (like ethernet). With TCP the kernel might also decide to join small data so that they get transmitted together.

Note that for TCP the packet length on the wire is actually irrelevant for the application since TCP is a continuous data stream. With UDP this would be different, i.e. every send would result in a single IP message and would also be received as such single message by the recipient.