Thinking based on the maximum UDP datagram length and MTU value

Maximum UDP datagram length

   In theory, the maximum length of an IP datagram is 65535 bytes, which is limited by the 16-bit total length field in the IP header (Figure IP protocol format). Remove the 20-byte IP header (Figure IP protocol format) and 8-byte UDP (Figure UDP protocol format) header. The maximum length of the user in the UDP datagram is 65507. However, most implementations provide a length greater than this The maximum value is small.

   

   

     We will encounter two limiting factors.

      First, the application may be restricted by its program interface. The socket API provides a function that applications can call to set the length of the receive and send buffers. For UDP sockets, this length is directly related to the length of the largest UDP datagram that the application can read and write. Most systems now provide their own UDP datagrams that can read and write more than 8192 by default.

      The second limitation comes from the kernel implementation of TCP / IP. There may be some implementation characteristics (or errors) that make the IP datagram length less than 65535 bytes.

      Test udp maximum datagram length code

      server.c

      

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>

#define SERVER_PORT 8888
#define MAX_MSG_SIZE 1024

void udps_respon (int sockfd)
{
    struct sockaddr_in addr;
    int addrlen, n;
    char msg [MAX_MSG_SIZE];
    
    while (1) {
        / * read from the network and write to the network * /
        bzero (msg, sizeof (msg)) ; // Initialize, set 0
        addrlen = sizeof (struct sockaddr);
        n = recvfrom (sockfd, msg, MAX_MSG_SIZE, 0, (struct sockaddr *) & addr, & addrlen); // Receive data from the client
        msg [n] = ' \ 0 ';
        / * Display the server has received the message * /
        fprintf (stdout, "Server have received% s", msg); // Display the message
    } 
}

int main(int argc,char *argv[])
{
    int sockfd;
    struct sockaddr_in addr;
    
    /*建立sockfd描述符*/
        sockfd = socket(AF_INET,SOCK_DGRAM,0);
        if(sockfd < 0){
                fprintf(stderr,"Socket Error:%s\n",strerror(errno));
                exit(1);
        }

    / * Server-side filled sockaddr structure * /
    bzero (& addr, sizeof (struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl (INADDR_ANY);
    addr.sin_port = htons (SERVER_PORT);
    
    / * Bundled sockfd Descriptor * /
    if (bind (sockfd, (struct sockaddr *) & addr, sizeof (struct sockaddr_in)) <0) {
        fprintf (stderr, "Bind Error:% s \ n", strerror (errno));
        exit (1 );
    }
    printf ("udp server start ...... \ n");
    udps_respon (sockfd); // Read and write operations
    close (sockfd);
}
 

client.c

   

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>

#define SERVER_PORT 8888
#define MAX_BUF_SIZE 65508

void udpc_requ(int sockfd,const struct sockaddr_in *addr,int len)
{
    char buffer[MAX_BUF_SIZE+1];
    int n;
    int i = 0;
    //while(1){
        /*从键盘读入,写到服务器*/
        printf("Please input char:\n");
        memset(buffer,0,sizeof(buffer));
        //fgets(buffer,MAX_BUF_SIZE,stdin);
        for(i = 0; i<MAX_BUF_SIZE; i ++)
        {
            buffer[i]='a';
        }
        n = sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr*)addr,len);
        //bzero(buffer,MAX_BUF_SIZE);
        printf("send bytes:%d\n",n);
    //}
}

int main(int argc,char *argv[])
{
    int sockfd;
    struct sockaddr_in addr;
    
    if(argc != 2){
        fprintf(stderr,"Usage:%s server_ip\n",argv[0]);
        exit(1);
    }
    
    /*建立sockfd描述符*/
    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd < 0){
        fprintf(stderr,"Socket Error:%s\n",strerror(errno));
        exit(1);
    }

    / * Fill the server data * /
    bzero (& addr, sizeof (struct sockaddr_in)); // Initialize, set 0
    addr.sin_family = AF_INET;
    addr.sin_port = htons (SERVER_PORT);
    if (inet_aton (argv [1], & addr .sin_addr) <0) {/ * inet_aton function is used to convert the string type IP address to the network type IP address * /
        fprintf (stderr, "Ip Error:% s \ n", strerror (errno));
        exit ( 1);
    }
    
    udpc_requ (sockfd, & addr, sizeof (struct sockaddr_in)); // Read and write operations
    close (sockfd);
}
 

HUMAN 值

    Refer to TCP / IP for detailed explanation of Volume I, Global Internet Experiments

     As an experiment, we run the modified traceroute program many times. The destination is hosts around the world, which can reach 15 countries (including Antarctica), using multiple transatlantic and transpacific links. However, before doing so, the dial-up SLIP link MTU between the author's subnet and the router netb was increased to 1500, which is the same as Ethernet.

    Among the 18 runs, only two of them found that the path MTU was less than 1500. One of the trans-Atlantic links has an MTU value of 572, and the router returns a new format ICMP error message. The other link, between two routers in Japan, cannot process 1500-byte data frames, and the router does not return ICMP error messages in the new format. Set the MTU value to 1006 to work normally.

      From this experiment, we can read the conclusion that many but not all WANs can now handle packets larger than 512. Using the path MTU discovery mechanism, applications can make full use of the larger MTU to send packets.

     For the MTU value, the host must be able to receive at least 576 bytes of IP datagrams. In many UDP application design, the application data is limited to 512 bytes or less, so it is smaller than this limit value. Of course, we can always send less than 512 bytes of data by analyzing many routing protocols (DNS TFTP BOOTP SNMP).

   

 

 

         

78 original articles published · 32 praised · 120,000 views

Guess you like

Origin blog.csdn.net/caofengtao1314/article/details/99713270