Heim Backend-Entwicklung PHP-Tutorial Php socket数据编码

Php socket数据编码

Jun 23, 2016 pm 01:46 PM

bytes.php  字节编码类

/** * byte数组与字符串转化类 * @author  * created on 2011-7-15 */class bytes {       /**     * 转换一个string字符串为byte数组     * @param $str 需要转换的字符串     * @param $bytes 目标byte数组     * @author zikie     */        public static function getbytes($str) {        $len = strlen($str);        $bytes = array();           for($i=0;$i= 128){                   $byte = ord($str[$i]) - 256;               }else{                   $byte = ord($str[$i]);               }            $bytes[] =  $byte ;        }        return $bytes;    }       /**     * 将字节数组转化为string类型的数据     * @param $bytes 字节数组     * @param $str 目标字符串     * @return 一个string类型的数据     */        public static function tostr($bytes) {        $str = '';        foreach($bytes as $ch) {            $str .= chr($ch);        }           return $str;    }       /**     * 转换一个int为byte数组     * @param $byt 目标byte数组     * @param $val 需要转换的字符串     * @author zikie     */       public static function integertobytes($val) {        $byt = array();        $byt[0] = ($val & 0xff);        $byt[1] = ($val >> 8 & 0xff);        $byt[2] = ($val >> 16 & 0xff);        $byt[3] = ($val >> 24 & 0xff);        return $byt;    }       /**     * 从字节数组中指定的位置读取一个integer类型的数据     * @param $bytes 字节数组     * @param $position 指定的开始位置     * @return 一个integer类型的数据     */        public static function bytestointeger($bytes, $position) {        $val = 0;        $val = $bytes[$position + 3] & 0xff;        $val > 8 & 0xff);        return $byt;    }       /**     * 从字节数组中指定的位置读取一个short类型的数据。     * @param $bytes 字节数组     * @param $position 指定的开始位置     * @return 一个short类型的数据     */        public static function bytestoshort($bytes, $position) {        $val = 0;        $val = $bytes[$position + 1] & 0xff;        $val = $val    <p class="sycode">       </p>    <p>  </p>  <p> socket.class.php  socket赋值类</p>  <p class="sycode">   </p><p class="sycode">       </p>   <pre style="代码" class="precsshei"><?phpdefine ("CONNECTED", true);define("DISCONNECTED", false);/** * Socket class *  *   * @author Seven  */Class Socket{    private static $instance;    private $connection = null;        private $connectionState = DISCONNECTED;        private $defaultHost = "127.0.0.1";        private $defaultPort = 80;        private $defaultTimeout = 10;        public  $debug = false;        function __construct()    {            }    /**     * Singleton pattern. Returns the same instance to all callers     *     * @return Socket     */    public static function singleton()    {        if (self::$instance == null || ! self::$instance instanceof Socket)        {            self::$instance = new Socket();                   }        return self::$instance;    }    /**     * Connects to the socket with the given address and port     *      * @return void     */    public function connect($serverHost=false, $serverPort=false, $timeOut=false)    {                if($serverHost == false)        {            $serverHost = $this->defaultHost;        }                if($serverPort == false)        {            $serverPort = $this->defaultPort;        }        $this->defaultHost = $serverHost;        $this->defaultPort = $serverPort;                if($timeOut == false)        {            $timeOut = $this->defaultTimeout;        }        $this->connection = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);                 if(socket_connect($this->connection,$serverHost,$serverPort) == false)        {            $errorString = socket_strerror(socket_last_error($this->connection));            $this->_throwError("Connecting to {$serverHost}:{$serverPort} failed.<br>Reason: {$errorString}");        }else{            $this->_throwMsg("Socket connected!");        }                $this->connectionState = CONNECTED;    }        /**     * Disconnects from the server     *      * @return True on succes, false if the connection was already closed     */    public function disconnect()    {        if($this->validateConnection())        {            socket_close($this->connection);            $this->connectionState = DISCONNECTED;            $this->_throwMsg("Socket disconnected!");            return true;        }        return false;    }    /**     * Sends a command to the server     *      * @return string Server response     */    public function sendRequest($command)    {        if($this->validateConnection())        {            $result = socket_write($this->connection,$command,strlen($command));            return $result;        }        $this->_throwError("Sending command \"{$command}\" failed.<br>Reason: Not connected");    }                public function isConn()    {        return $this->connectionState;    }            public function getUnreadBytes()    {                $info = socket_get_status($this->connection);        return $info['unread_bytes'];    }        public function getConnName(&$addr, &$port)    {        if ($this->validateConnection())        {            socket_getsockname($this->connection,$addr,$port);        }    }               /**     * Gets the server response (not multilined)     *      * @return string Server response     */    public function getResponse()    {        $read_set = array($this->connection);            while (($events = socket_select($read_set, $write_set = NULL, $exception_set = NULL, 0)) !== false)         {            if ($events > 0)            {                foreach ($read_set as $so)                {                    if (!is_resource($so))                    {                        $this->_throwError("Receiving response from server failed.<br>Reason: Not connected");                        return false;                    }elseif ( ( $ret = @socket_read($so,4096,PHP_BINARY_READ) ) == false){                        $this->_throwError("Receiving response from server failed.<br>Reason: Not bytes to read");                        return false;                    }                    return $ret;                }            }        }                return false;    }    public function waitForResponse()    {        if($this->validateConnection())        {            return socket_read($this->connection, 2048);        }                $this->_throwError("Receiving response from server failed.<br>Reason: Not connected");    return false;    }    /**     * Validates the connection state     *      * @return bool     */    private function validateConnection()    {        return (is_resource($this->connection) && ($this->connectionState != DISCONNECTED));    }    /**     * Throws an error     *      * @return void     */    private function _throwError($errorMessage)    {        echo "Socket error: " . $errorMessage;    }    /**     * Throws an message     *      * @return void     */    private function _throwMsg($msg)    {        if ($this->debug)        {            echo "Socket message: " . $msg . "\n\n";        }    }    /**     * If there still was a connection alive, disconnect it     */    public function __destruct()    {        $this->disconnect();    }}?>
Nach dem Login kopieren

PacketBase.class.php  打包类

<?php /** * PacketBase class *  * 用以处理与c++服务端交互的sockets 包 *  * 注意:不支持宽字符 *  * @author Seven <seven@qoolu.com> *  */class PacketBase extends ContentHandler{    private $head;    private $params;    private $opcode;    /**************************construct***************************/    function __construct()    {        $num = func_num_args();        $args = func_get_args();        switch($num){                case 0:                    //do nothing 用来生成对象的                break;                case 1:                        $this->__call('__construct1', $args);                        break;                case 2:                        $this->__call('__construct2', $args);                        break;                default:                        throw new Exception();        }    }    //无参数    public function __construct1($OPCODE)    {            $this->opcode = $OPCODE;            $this->params = 0;    }    //有参数    public function __construct2($OPCODE,  $PARAMS)    {            $this->opcode = $OPCODE;            $this->params = $PARAMS;    }    //析构    function __destruct()    {        unset($this->head);        unset($this->buf);    }        //打包    public function pack()    {        $head = $this->MakeHead($this->opcode,$this->params);        return $head.$this->buf;    }    //解包    public function unpack($packet,$noHead = false)    {                $this->buf = $packet;        if (!$noHead){        $recvHead = unpack("S2hd/I2pa",$packet);        $SD = $recvHead[hd1];//SD        $this->contentlen = $recvHead[hd2];//content len        $this->opcode = $recvHead[pa1];//opcode        $this->params = $recvHead[pa2];//params                $this->pos = 12;//去除包头长度                if ($SD != 21316)        {            return false;        }        }else         {            $this->pos = 0;        }        return true;       }    public function GetOP()    {        if ($this->buf)        {            return $this->opcode;        }        return 0;    }    /************************private***************************/    //构造包头    private function MakeHead($opcode,$param)    {        return pack("SSII","SD",$this->TellPut(),$opcode,$param);    }        //用以模拟函数重载    private function __call($name, $arg)    {        return call_user_func_array(array($this, $name), $arg);    }            /***********************Uitl***************************/    //将16进制的op转成10进制    static function MakeOpcode($MAJOR_OP, $MINOR_OP)    {        return ((($MAJOR_OP & 0xffff) buf = "";        $this->contentlen = 0;        $this->pos = 0;    }    function __destruct()    {        unset($this->buf);    }        public function PutInt($int)    {        $this->buf .= pack("i",(int)$int);    }    public function PutUTF($str)    {        $l = strlen($str);        $this->buf .= pack("s",$l);        $this->buf .= $str;    }    public function PutStr($str)    {        return $this->PutUTF($str);    }            public function TellPut()    {        return strlen($this->buf);    }            /*******************************************/        public function GetInt()    {        //$cont = substr($out,$l,4);        $get = unpack("@".$this->pos."/i",$this->buf);        if (is_int($get[1])){            $this->pos += 4;            return $get[1];        }        return 0;    }      public function GetShort()    {        $get = unpack("@".$this->pos."/S",$this->buf);        if (is_int($get[1])){            $this->pos += 2;            return $get[1];        }        return 0;    }    public function GetUTF()    {        $getStrLen = $this->GetShort();                if ($getStrLen > 0)        {            $end = substr($this->buf,$this->pos,$getStrLen);            $this->pos += $getStrLen;            return $end;        }        return '';    }    /***************************/      public function GetBuf()    {        return $this->buf;    }        public function SetBuf($strBuf)    {        $this->buf = $strBuf;    }        public function ResetBuf(){        $this->buf = "";        $this->contentlen = 0;        $this->pos = 0;    }}?>
Nach dem Login kopieren

格式

struct header{int type;     // 消息类型int length;     // 消息长度}struct MSG_Q2R2DB_PAYRESULT{int serialno; int openid; char payitem[512];int billno; int zoneid;int providetype;  int coins; }调用的方法,另外需require两个php文件,一个是字节编码类,另外一个socket封装类,其实主要看字节编码类就可以了!
Nach dem Login kopieren

调用测试

public function index() {        $socketAddr = "127.0.0.1";            $socketPort = "10000";            try {                        $selfPath = dirname ( __FILE__ );            require ($selfPath . "/../Tool/Bytes.php");            $bytes = new Bytes ();                        $payitem = "sdfasdfasdfsdfsdfsdfsdfsdfsdf";            $serialno = 1;            $zoneid = 22;            $openid = "CFF47C448D4AA2069361567B6F8299C2";                        $billno = 1;            $providetype = 1;            $coins = 1;                        $headType = 10001;            $headLength = 56 + intval(strlen($payitem ));                        $headType = $bytes->integerToBytes ( intval ( $headType ) );            $headLength = $bytes->integerToBytes ( intval ( $headLength ) );            $serialno = $bytes->integerToBytes ( intval ( $serialno ) );            $zoneid = $bytes->integerToBytes ( intval ( $zoneid ) );            $openid = $bytes->getBytes( $openid  );            $payitem_len = $bytes->integerToBytes ( intval ( strlen($payitem) ) );            $payitem =  $bytes->getBytes($payitem);                        $billno = $bytes->integerToBytes ( intval ( $billno ) );            $providetype = $bytes->integerToBytes ( intval ( $providetype ) );            $coins = $bytes->integerToBytes ( intval ( $coins ) );                        $return_betys = array_merge ($headType , $headLength , $serialno , $zoneid , $openid,$payitem_len ,$payitem,$billno,$providetype,$coins);                        $msg = $bytes->toStr ($return_betys);            $strLen = strlen($msg);            $packet = pack("a{$strLen}", $msg);            $pckLen = strlen($packet);                        $socket = Socket::singleton ();            $socket->connect ( $socketAddr, $socketPort ); //连服务器                        $sockResult = $socket->sendRequest ( $packet); // 将包发送给服务器             sleep ( 3 );            $socket->disconnect (); //关闭链接        } catch ( Exception $e ) {            var_dump($e);            $this->log_error("pay order send to server".$e->getMessage());        }    }
Nach dem Login kopieren


Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Mar 14, 2025 am 11:42 AM

Die PHP Client -URL -Erweiterung (CURL) ist ein leistungsstarkes Tool für Entwickler, das eine nahtlose Interaktion mit Remote -Servern und REST -APIs ermöglicht. Durch die Nutzung von Libcurl, einer angesehenen Bibliothek mit Multi-Protokoll-Dateien, erleichtert PHP Curl effiziente Execu

12 Beste PHP -Chat -Skripte auf Codecanyon 12 Beste PHP -Chat -Skripte auf Codecanyon Mar 13, 2025 pm 12:08 PM

Möchten Sie den dringlichsten Problemen Ihrer Kunden in Echtzeit und Sofortlösungen anbieten? Mit Live-Chat können Sie Echtzeitgespräche mit Kunden führen und ihre Probleme sofort lösen. Sie ermöglichen es Ihnen, Ihrem Brauch einen schnelleren Service zu bieten

Erklären Sie das Konzept der späten statischen Bindung in PHP. Erklären Sie das Konzept der späten statischen Bindung in PHP. Mar 21, 2025 pm 01:33 PM

In Artikel wird die in PHP 5.3 eingeführte LSB -Bindung (LSB) erörtert, die die Laufzeitauflösung der statischen Methode ermöglicht, um eine flexiblere Vererbung zu erfordern. Die praktischen Anwendungen und potenziellen Perfo von LSB

Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Rahmensicherheitsmerkmale: Schutz vor Schwachstellen. Rahmensicherheitsmerkmale: Schutz vor Schwachstellen. Mar 28, 2025 pm 05:11 PM

In Artikel werden wichtige Sicherheitsfunktionen in Frameworks erörtert, um vor Schwachstellen zu schützen, einschließlich Eingabevalidierung, Authentifizierung und regelmäßigen Aktualisierungen.

Anpassung/Erweiterung von Frameworks: So fügen Sie benutzerdefinierte Funktionen hinzu. Anpassung/Erweiterung von Frameworks: So fügen Sie benutzerdefinierte Funktionen hinzu. Mar 28, 2025 pm 05:12 PM

In dem Artikel werden Frameworks hinzugefügt, das sich auf das Verständnis der Architektur, das Identifizieren von Erweiterungspunkten und Best Practices für die Integration und Debuggierung hinzufügen.

Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Apr 01, 2025 pm 03:12 PM

Senden von JSON -Daten mithilfe der Curl -Bibliothek von PHP in der PHP -Entwicklung müssen häufig mit externen APIs interagieren. Eine der gängigen Möglichkeiten besteht darin, die Curl Library zu verwenden, um Post � ...

See all articles