Home  >  Article  >  Backend Development  >  PHP implements the method of sending hexadecimal socket

PHP implements the method of sending hexadecimal socket

小云云
小云云Original
2018-03-31 15:25:433121browse

This article mainly shares with you the method of sending hexadecimal socket in PHP. It is mainly shared with you in the form of code. I hope it can help you.

<?php
        //~ $sendStr = &#39;7d 2c 00 00 00 00&#39;;  // 16进制数据
        $sendStr = "\x7D\x2c\x00\x00\x00\x00";  // 16进制数据
        
        //~ $sendStrArray = str_split(str_replace(&#39; &#39;, &#39;&#39;, $sendStr), 2);  // 将16进制数据转换成两个一组的数组


        $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));  // 创建Socket


        if (socket_connect($socket, "127.0.0.1", 50001)) {  //连接
socket_write($socket, $sendStr);  // 逐组数据发送
            //~ for ($j = 0; $j < count($sendStrArray); $j++) {
                //~ socket_write($socket, chr(hexdec($sendStrArray[$j])));  // 逐组数据发送
            //~ }


            $receiveStr = "";
            $receiveStr = socket_read($socket, 1024, PHP_BINARY_READ);  // 采用2进制方式接收数据
            $receiveStrHex = bin2hex($receiveStr);  // 将2进制数据转换成16进制


            echo "client:" . $receiveStrHex;
        }
        socket_close($socket);  // 关闭Socket
        ?>

Related recommendations:

How to implement socket in php

Detailed example of socket programming in php

Detailed explanation of socket communication in php

The above is the detailed content of PHP implements the method of sending hexadecimal socket. For more information, please follow other related articles on the PHP Chinese website!

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