1.
<?php //网络字节序是指大端序,TCP/IP都是采用网络字节序的方式。 //======================================================================================================// //n -- 无符号短整数 (16位, 大端字节序) $str = 'test.string'; $pack = pack('n', strlen($str)); echo strlen($pack); // 2个字节,此值是确定的 $unpack = unpack('nLen', $pack); // Len相当于定义的一个变量,返回值为打包时的长度 echo $unpack['Len']; // 等于strlen($str) 11个字节 //exit; //======================================================================================================// //N -- 无符号长整数 (32位, 大端字节序) $str = 'test.string'; $pack = pack('N', strlen($str)); echo strlen($pack); // 4个字节,此值是确定的 $unpack = unpack('NLen', $pack); // Len相当于定义的一个变量,返回值为打包时的长度 echo $unpack['Len']; // 等于strlen($str) 11个字节 exit; //======================================================================================================// //如果提供socket服务方定义的 //请求边界标识的长度为2,则用unsigned 16位的 ‘n’ //请求边界标识的长度为4,则用unsigned 32位的 ‘N’ //那么整个包体为 【网络字节序】+数据包实体,附加发送的长度参数为:【数据包实体长度+网络字节序长度】 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $connect = socket_connect($socket, '192.168.1.10', 55660); $buffer = '我是数据包实体'; $length = strlen($buffer); $pack = pack("N", $length + 4); socket_write($socket, $pack.$buffer, $length + 4); //同样unpack出来的lenght也是等于strlen($buffer)+4 //那么socket_read之后的返回数据包就需要去掉4个长度就可以了
2.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $conn = socket_connect($socket, '192.168.1.10', 55660); $buffer = 'test.string'; $len = strlen($buffer); echo 'buffter length: '.$len.' '; $pack = pack('n', $len + 2); $send = socket_write($socket, $pack.$buffer, $len + 2); if($send){ echo 'send ok '; } $read = socket_read($socket, 10240); echo 'read data:'.$read .' '; echo 'read length:'.strlen($read).' '; echo 'real data is:'.substr($read, 2);
3.
<?php class AndyHua extends Man\Core\SocketWorker{ public function dealInput($recv){ //目前已经收到的长度 $len = strlen($recv); //preread_length = 2 if($len < 2){ return 2 - $len; } $t = unpack('nLen', $recv); //Len为包的实际长度 return $t['Len'] - $len; //如果$t['Len']等于$len那就说明已经接收完了 } public function dealProcess($recv){ return $this->sendToClient($recv); //return $this->sendToClient(substr($recv, 2)); } }
4. [图片] QQ截图20141114144433.png
5. [图片] QQ截图20141114144329.png
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks agoByDDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks agoByDDD
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor