PHP socket 接收 java端口 netty 网络字节序

摘要:
˃PHP本地测试:/***@authorsukura*/$bytes=B

java 服务端测试代码:

@Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throwsException {

        buffer.writeShort(5);
        buffer.writeInt(-51321);
        buffer.writeFloat(-123);
        buffer.writeDouble(-1121);
        buffer.writeBytes("测试测试ing123".getBytes());


        ctx.write(buffer, promise);

    }

PHP 端接收代码:

<?php

$host = 'xxxx';
$port = 9876;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket.
");
$connection = socket_connect($socket, $host, $port) or die("Could not connect server.
");
//socket_write($socket, json_encode(['age'=>"333", 'name'=>'aaa', "bb"=>121])) or die("Write failed.
");

$ret = socket_recv($socket, $msg, 130,MSG_PEEK  );


$bytes = Bytes::initBytes($msg);
$short = $bytes->readShort();
$int = $bytes->readInt();
$float = $bytes->readFloat();
$double = $bytes->readDouble();
$string = $bytes->readString(100);

var_dump($double);die();


classBytes{

    private $buffer = null;
    private $readIdx= 0;
    private $maxLength = 0;

    const BIG_ENDIAN  = 1;
    const LITTLE_ENDIAN = 2;
    const WAITING_CHECK = -1;
    private static $result = -1;

    const INT_LEN = 4;
    const SHORT_LEN= 2;
    const FLOAT_LEN = 4;
    const DOUBLE_LEN = 8;

    //判断本地字节序
    public static functiongetEndian(){

        if( static::$result == static::WAITING_CHECK ){
            static::$result = self::LITTLE_ENDIAN;
            if(  pack('L', 1) === pack('N', 1) ){
                static::$result == static::BIG_ENDIAN;
            }
        }
        return static::$result;
    }


    public static function initBytes( $buffers){
        return new static( $buffers);
    }

    private function __construct($buff){
        $this->buffer = $buff;
        $this->maxLength = strlen($buff);
    }

    public functionreadShort(){
        $short = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $short = unpack( 's' ,  substr( $this->buffer, $this->readIdx, static::SHORT_LEN )  );
        }else{
            $short = unpack( 's', strrev( substr( $this->buffer, $this->readIdx, static::SHORT_LEN ) ));
        }
        $this->updateReadIdx(static::SHORT_LEN);
        return $short;
    }

    public functionreadInt(){
        $int = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $int = unpack( 'i' ,  substr( $this->buffer, $this->readIdx, static::INT_LEN )  );
        }else{
            $int = unpack( 'i', strrev( substr( $this->buffer, $this->readIdx, static::INT_LEN ) ));
        }
        $this->updateReadIdx(static::INT_LEN);
        return $int;
    }

    public function readString( $length = 100){
        $str = "";
        $len = min( $length, $this->maxLength - $this->readIdx );
        for( $i = $this->readIdx; $i < $this->readIdx+$len; $i++){
            $str .=  $this->buffer[$i];
        }
        $this->updateReadIdx($len);
        return $str;
    }


    public functionreadFloat(){

        $float = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $float = unpack( 'f' ,  substr( $this->buffer, $this->readIdx, static::FLOAT_LEN )  );
        }else{
            $float = unpack( 'f', strrev( substr( $this->buffer, $this->readIdx, static::FLOAT_LEN ) ));
        }
        $this->updateReadIdx(static::FLOAT_LEN);
        return $float;

    }

    public functionreadDouble(){
        $double = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $double = unpack( 'd' ,  substr( $this->buffer, $this->readIdx, static::DOUBLE_LEN )  );
        }else{
            $double = unpack( 'd', strrev( substr( $this->buffer, $this->readIdx, static::DOUBLE_LEN ) ));
        }
        $this->updateReadIdx(static::DOUBLE_LEN);
        return $double;
    }

    private function updateReadIdx( $length){
        $this->readIdx += $length;
    }

}


?>

PHP 本地测试 :

/**
 * @author sukura
 */

$bytes = Bytes::initBytes();
$bytes->writeDouble(-5.123)->writeInt(5.3)->writeFloat(-12.123);
var_dump($bytes->readDouble());
var_dump($bytes->readInt());
var_dump($bytes->readFloat());


classBytes{

    private $buffer = null;
    private $readIdx= 0;
    private $maxLength = 0;

    const BIG_ENDIAN  = 1;
    const LITTLE_ENDIAN = 2;
    const WAITING_CHECK = -1;
    private static $result = -1;

    const INT_LEN = 4;
    const SHORT_LEN= 2;
    const FLOAT_LEN = 4;
    const DOUBLE_LEN = 8;

    public static functiongetEndian(){

        if( static::$result == static::WAITING_CHECK ){
            static::$result = self::LITTLE_ENDIAN;
            if(  pack('L', 1) === pack('N', 1) ){
                static::$result == static::BIG_ENDIAN;
            }
        }
        return static::$result;
    }


    public static function initBytes( $buffers = null){
        return new static( $buffers);
    }

    private function __construct( $buff = null){
        $this->buffer = $buff;
        $this->maxLength = strlen($buff);
    }

    public functionreadShort(){
        $short = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $short = unpack( 's' ,  substr( $this->buffer, $this->readIdx, static::SHORT_LEN )  );
        }else{
            $short = unpack( 's', strrev( substr( $this->buffer, $this->readIdx, static::SHORT_LEN ) ));
        }
        $this->updateReadIdx(static::SHORT_LEN);
        return $short;
    }

    public functionreadInt(){
        $int = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $int = unpack( 'i' ,  substr( $this->buffer, $this->readIdx, static::INT_LEN )  );
        }else{
            $int = unpack( 'i', strrev( substr( $this->buffer, $this->readIdx, static::INT_LEN ) ));
        }
        $this->updateReadIdx(static::INT_LEN);
        return $int;
    }

    public function readString( $length = 100){
        $str = "";
        $len = min( $length, $this->maxLength - $this->readIdx );
        for( $i = $this->readIdx; $i < $this->readIdx+$len; $i++){
            $str .=  $this->buffer[$i];
        }
        $this->updateReadIdx($len);
        return $str;
    }


    public functionreadFloat(){

        $float = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $float = unpack( 'f' ,  substr( $this->buffer, $this->readIdx, static::FLOAT_LEN )  );
        }else{
            $float = unpack( 'f', strrev( substr( $this->buffer, $this->readIdx, static::FLOAT_LEN ) ));
        }
        $this->updateReadIdx(static::FLOAT_LEN);
        return $float;

    }

    public functionreadDouble(){
        $double = 0;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $double = unpack( 'd' ,  substr( $this->buffer, $this->readIdx, static::DOUBLE_LEN )  );
        }else{
            $double = unpack( 'd', strrev( substr( $this->buffer, $this->readIdx, static::DOUBLE_LEN ) ));
        }
        $this->updateReadIdx(static::DOUBLE_LEN);
        return $double;
    }

    public function writeInt( $intVal){
        $byte = null;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $byte = pack('i',  $intVal);
        }else{
            $byte = strrev( pack( 'i' , $intVal) );
        }
        $this->mergeByte($byte);
        return $this;
    }


    public function writeFloat( $floatVal){
        $byte = null;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $byte = pack('f',  $floatVal);
        }else{
            $byte = strrev( pack( 'f' , $floatVal) );
        }
        $this->mergeByte($byte);
        return $this;
    }

    public function writeString( $strVal){
        $byte = null;
        $len = strlen($strVal);
        $formate = "a{$len}";
        if( static::getEndian() == static::BIG_ENDIAN ){
            $byte = pack($formate,  $strVal);
        }else{
            $byte = strrev( pack( $formate , $strVal) );
        }
        $this->mergeByte($byte);
        return $this;
    }


    public function writeDouble( $doubleVal){
        $byte = null;
        if( static::getEndian() == static::BIG_ENDIAN ){
            $byte = pack('d',  $doubleVal);
        }else{
            $byte = strrev( pack( 'd' , $doubleVal) );
        }
        $this->mergeByte($byte);
        return $this;
    }

    private function mergeByte( $bytes){
        return $this->buffer .= $bytes;
    }

    private function updateReadIdx( $length){
        $this->readIdx += $length;
    }

}

免责声明:文章转载自《PHP socket 接收 java端口 netty 网络字节序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Swoole从入门到入土(4)——TCP服务器[正确重启]4.Nginx负载均衡下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux内核 TCP/IP、Socket参数调优

/proc/sys/net目录所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这些重要的参数: 参数(路径+文件) 描述 默认值 优化值 /proc/sys/net/core/rmem_default 默认的TCP数据接收窗口大小(...

C# Socket服务器端如何判断客户端断开

使用Socket类中的Poll方法,就可以。 Socket client //假如已经创建好了,连接到服务器端得Socket的客户端对象。 我们只要client.Poll(10,SelectMode.SelectRead)判断就行了。只要返回True是。就可以认为客户端已经断开了。 Poll 方法将会检查 Socket 的状态。指定 selectMode...

(经典)tcp粘包分析

转载自csdn:http://blog.csdn.net/zhangxinrun/article/details/6721495 这两天看csdn有一些关于socket粘包,socket缓冲区设置的问题,发现自己不是很清楚,所以查资料了解记录一下: 一 .两个简单概念长连接与短连接:1.长连接     Client方与Server方先建立通讯连接,连接建...

C++ STL hash表用法

C++ STL unordered_map用法 在C++11中,unordered_map作为一种关联容器,替代了hash_map,unordered_map的底层实现是hash表,所以被称为无序关联容器。不管是map还是unordered_map都是一种 key-map(value) 映射的容器,提供非常高的查找效率,下面我们来了解unordered_m...

Linux 7.6查看开机自启

[root@tz-yycs-db01 etc]# systemctl list-unit-files |grep enable brandbot.path enabled auditd.service enabled autov...

给HttpClient添加Socks代理

本文描述http client使用socks代理过程中需要注意的几个方面:1,socks5支持用户密码授权;2,支持https;3,支持让代理服务器解析DNS; 使用代理创建Socket 从原理上来看,不管用什么http客户端(httpclient,okhttp),最终都要转换到java.net.Socket的创建上去,看到代码: package java...