Home php教程 PHP源码 php socket编程|网络字节序,大端序|workerman

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

May 26, 2016 am 08:20 AM

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个长度就可以了
Copy after login


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

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

               

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

14144448_bV1G.png

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

14144448_aBLY.png

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)