Home php教程 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

Sep 14, 2018 am 10:12 AM

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;
}
Copy after login

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;
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)