search
HomeBackend DevelopmentPHP TutorialUDP校验和的计算方法

如何求UDP的校验和,我的IP是192.168.1.59  端口号是30002
连接的机子是192.168.1.179 端口号是30000

然后 是如何求校验和的呢


回复讨论(解决方案)

咕~~(???)b 不懂

    就是chechsum 如何获取到,还要有字节长度吗?还需要字节长度吗?  字节长度如果是66   然后 又怎么获取到 这个checksum呢?

这个一般是系统级或网卡做的事,php很难做吧?

嗯  php 这方面的不多, 而且这个校验和和传输的数据,所有的都相加, 又异或 ,再求和。。什么的。 但是  不知道 都要加哪些?

找个 C 语言的代码移植一下不就可以了

找个 C 语言的代码移植一下不就可以了
有没有 例子 什么的啊。  关键php中16进制的数,是要先转成二进制的,才能进行异或求和吗?

php 提供了所有的相关函数
你把你不会的部分贴出来

$FileData=array();

///8字节头
$FileData[0]=0x75;
$FileData[1]=0x30;
$FileData[2]=0x75;
$FileData[3]=0x32;
$FileData[4]=0x00;
$FileData[5]=0x20;

//------Udp 头部校验和




$a=$FileData[0]*256+$FileData[1];
$b=$FileData[2]*256+$FileData[3];
$c=$FileData[4]*256+$FileData[5];
$d=($a+$b+$c);

$FileData[6]=~(($d>>8)&0xFF);
$FileData[7]=~($d&0xFF);
//---------------------

///16字节数据
//标识号:01020304
$FileData[8]=0x01;
$FileData[9]=0x02;
$FileData[10]=0x03;
$FileData[11]=0x04;

//操作命令:0x00000001
$FileData[12]=0x00;
$FileData[13]=0x00;
$FileData[14]=0x00;
$FileData[15]=0x02;

//操作结果:0x00000000
$FileData[16]=0x00;
$FileData[17]=0x00;
$FileData[18]=0x00;
$FileData[19]=0x01;

//保留
$FileData[20]=0x00;
$FileData[21]=0x00;
$FileData[22]=0x00;
$FileData[23]=0x00;

//---------------------
///8字节校验和
$FileData[24]=$FileData[0]^$FileData[8]^$FileData[16];
$FileData[25]=$FileData[1]^$FileData[9]^$FileData[17];
$FileData[26]=$FileData[2]^$FileData[10]^$FileData[18];
$FileData[27]=$FileData[3]^$FileData[11]^$FileData[19];
$FileData[28]=$FileData[4]^$FileData[12]^$FileData[20];
$FileData[29]=$FileData[5]^$FileData[13]^$FileData[21];
$FileData[30]=$FileData[6]^$FileData[14]^$FileData[22];
$FileData[31]=$FileData[7]^$FileData[15]^$FileData[23];

$arrayLen = sizeof( $FileData );
foreach($FileData as $arr)
{
$str.= $arr;
}

echo $str;
//$str 应该是16字节的数据
UDP报文的数据内容定义
标识号 操作命令 操作数据(结果) 保留
4字节   4字节   4字节   4字节


所以现在就是想得到它的校验和

我上面写的校验和不对,输出的数据也不对

php 提供了所有的相关函数
你把你不会的部分贴出来

而且 IP如何转成16进制的?  这方面 也不会,

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools