1

I have written tcp client-server, server will read a socket, and client will written packets to that socket.

Mtu : 1500, packets will be fragmented if size exceds mtu.

Case 1: When i did write packet size 1456, 1457,1458,1459,14560 from client, recived the same size packets in server side.

Case 2: When i did write packet size 1461 from client, recived the same size packet, but this should be two packets (pkt-1 size :1460 + pkt-2 size :1)

Got a bit confused while dealing with fragmentations, can any one clarify this.

Edit : sock = socket(AF_INET, SOCK_STREAM, 0); <== socket being used.

2
  • What kind of socket? How do you define 'packet' ?
    – hertitu
    Aug 24 '16 at 14:59
  • sock = socket(AF_INET, SOCK_STREAM, 0);
    – Vasu
    Aug 25 '16 at 6:15
4

You are confusing MTU and MSS.

The MTU 1500 is related to ethernet, so it includes the IP and TCP headers.

Both have a minimum size of 20, so the MSS (maximum segment size) for TCP is in this case 1500 - 20 - 20 = 1460.

Note that both the IP header and the tcp header can have a size greater than 20, when using options, and so you may still encounter fragmentation with message shorter than 1460 bytes.

Additionally your packet may traverse other networks that have a different MTU. A VPN will also add a header that will further reduce the effective MSS.

2

When you use a TCP socket, as the name "SOCK_STREAM" indicates you are not writing individual packets to the socket, but a data stream, and the way TCP handles this is transparent to you. So if you write 1461 (or 1,000,000 for that matter) bytes to the stream, at the other end you will be able to read 1461 (or 1,000,000) bytes. TCP automagically takes care of chopping the stream op in segments (this is where the MSS comes into play), retransmitting lost segments, re-ordering segments that are received out-of-order etc. If you do a packet capture (e.g using tcpdump or wireshark) then you will see (part of) that, but to your process using the socket it is all invisible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.