Home  >  Article  >  php教程  >  php socket编程|网络字节序,大端序|workerman

php socket编程|网络字节序,大端序|workerman

PHP中文网
PHP中文网Original
2016-05-26 08:20:432602browse

1.

<?php
//网络字节序是指大端序,TCP/IP都是采用网络字节序的方式。

//======================================================================================================//

//n -- 无符号短整数 (16位, 大端字节序)

$str = &#39;test.string&#39;;
$pack = pack(&#39;n&#39;, strlen($str));

echo strlen($pack); // 2个字节,此值是确定的

$unpack = unpack(&#39;nLen&#39;, $pack); // Len相当于定义的一个变量,返回值为打包时的长度

echo $unpack[&#39;Len&#39;];  // 等于strlen($str) 11个字节

//exit;

//======================================================================================================//

//N -- 无符号长整数 (32位, 大端字节序)

$str = &#39;test.string&#39;;
$pack = pack(&#39;N&#39;, strlen($str));

echo strlen($pack); // 4个字节,此值是确定的

$unpack = unpack(&#39;NLen&#39;, $pack); // Len相当于定义的一个变量,返回值为打包时的长度

echo $unpack[&#39;Len&#39;];  // 等于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, &#39;192.168.1.10&#39;, 55660);

$buffer = &#39;我是数据包实体&#39;;
$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, &#39;192.168.1.10&#39;, 55660);

$buffer = &#39;test.string&#39;;
$len = strlen($buffer);
echo &#39;buffter length: &#39;.$len.&#39;
&#39;;
$pack = pack(&#39;n&#39;, $len + 2);
$send = socket_write($socket, $pack.$buffer, $len + 2);
if($send){
	echo &#39;send ok
&#39;;	
}

$read = socket_read($socket, 10240);

echo &#39;read data:&#39;.$read .&#39;
&#39;;
echo &#39;read length:&#39;.strlen($read).&#39;
&#39;;
echo &#39;real data is:&#39;.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(&#39;nLen&#39;, $recv);
		//Len为包的实际长度
		return $t[&#39;Len&#39;] - $len; 
		//如果$t[&#39;Len&#39;]等于$len那就说明已经接收完了
    }

    public function dealProcess($recv){

		return $this->sendToClient($recv);
		//return $this->sendToClient(substr($recv, 2));
		
    }
	
}

               

4. [图片] QQ截图20141114144433.pngphp socket编程|网络字节序,大端序|workerman    

14144448_bV1G.png

5. [图片] QQ截图20141114144329.pngphp socket编程|网络字节序,大端序|workerman    

14144448_aBLY.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
Previous article:PHP MVC 从零学起(1)Next article:单文件PHP框架