I tried the following code to find out maximum segment size in TCP and UDP sockets. I might be wrong because I am using IPPROTO_TCP and TCP_MAXSEG for SOCK_DGRAM but I’m getting a value and I want to know the reason.
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
perror("cant create socket");
aopt = sizeof(optval);
getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, (char *)&optval, &aopt);
printf("tcp max segment size is=%d\n", optval);
output:
tcp max segment size is=536
then I tried then same with an UDP socket:
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
perror("cant create socket");
aopt = sizeof(optval);
getsockopt(sockfd, IPPROTO_TCP, TCP_MAXSEG, (char *)&optval, &aopt);
printf("udp max segment size is=%d\n", optval);
output:
udp max segment size is=134514139
I am sure
getsockoptactually fails (and returns a non-zero status you are ignoring) so you are printing garbage (it’s likely you didn’t initializeoptval).EDIT
UDPdoesn’t have a MSS. The maximum size of a datagram is limited by many factors (how wiling is the OS to send a big datagram). The hard limit is the size IP can carry.IPhas 16bTotal Lengthfield. So I’m guessing if the OS lets you,65535 - iphdris the maximum size of a datagram.