Home  >  Article  >  php教程  >  Use actual program to test the maximum packet size of udp sendto function---65507

Use actual program to test the maximum packet size of udp sendto function---65507

坏嘻嘻
坏嘻嘻Original
2018-09-14 10:12:133790browse

The example of this article describes the UDP communication method of PHP's Socket communication. Share it with everyone for your reference. The details are as follows:

We know that the IP packet header has a length of 16 bits, and the corresponding binary maximum value is 2^16 -1, which means that the maximum value of the entire length of an IP packet is 2^16 - 1 words section, if UDP communication is considered, then after removing the 20 bytes of the IP header and the 8 bytes of the UDP header, there are still 2^16 - 1 - 20 - 8 bytes left. Let’s play with the program (this article only uses the client to send data as an example).

Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
 
int main(int argc, char *argv[])
{
    struct sockaddr_in srvAddr;
    bzero(&srvAddr, sizeof(srvAddr));
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    srvAddr.sin_port = htons(8888);
 
    int iSock = socket(AF_INET, SOCK_DGRAM, 0); // udp
	char szBuf[1024 * 64 -1 - 20 - 8] = {0};
    int iRet = sendto(iSock, szBuf, sizeof(szBuf), 0, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
	printf("send size is %d, iRet is %d, errmsg[%s]\n", sizeof(szBuf), iRet, strerror(errno));
    
	close(iSock);
    return 0;
}

Result:

send size is 65507, iRet is 65507, errmsg[Success]

Okay, let’s make the send size larger by 1 byte. Try:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
 
int main(int argc, char *argv[])
{
    struct sockaddr_in srvAddr;
    bzero(&srvAddr, sizeof(srvAddr));
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    srvAddr.sin_port = htons(8888);
 
    int iSock = socket(AF_INET, SOCK_DGRAM, 0); // udp
	char szBuf[1024 * 64 - 20 - 8] = {0};
    int iRet = sendto(iSock, szBuf, sizeof(szBuf), 0, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
	printf("send size is %d, iRet is %d, errmsg[%s]\n", sizeof(szBuf), iRet, strerror(errno));
    
	close(iSock);
    return 0;
}

Result:

send size is 65508, iRet is -1, errmsg[Message too long]

Related recommendations:

php socket communication (tcp/udp) example analysis_php skills

PHP Socket communication UDP communication example_php skills

The above is the detailed content of Use actual program to test the maximum packet size of udp sendto function---65507. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Swift study notes 1 hello worldNext article:None