duangouyan3328 2016-09-12 07:05
浏览 53
已采纳

如何将数组包发送到socket

I want to write and read a message on socket in PHP
My write message is = 82,10,1,6,150,249
My read message is = 82,10,1,6,148,297
I write this code but do not wrok.
Tips: my message is array

 $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

$message = array('1'=> '82', '2'=>'10', '3'=>'1' ,'4'=>'6' ,'5'=>'150' ,'6'=>'249');
$len = strlen($message);

socket_sendto($sock, $message, $len, 0, '192.168.43.232', 5050);
socket_close($sock)
  • 写回答

1条回答 默认 最新

  • dqy012345 2016-09-24 09:21
    关注
        $host = "192.168.1.1";
        $port = 8080;
    
    
        //Create Packet
        $TurnOn = array(82, 10, 1, 6, 150, 249);
        foreach ($TurnOn as $chunk) {
            $message .= chr($chunk);
        }
    
        //Create socket
        $socket = socket_create(AF_INET, SOCK_STREAM, 0);
        if (!$socket) {
            $errorcode = socket_last_error();
            $errormsg = socket_strerror($errorcode);
    
            die("Couldn't create socket: [$errorcode] $errormsg 
    ");
        }
    
    
        //Connect socket to remote server
        if (!socket_connect($socket, $host, $port)) {
            $errorcode = socket_last_error();
            $errormsg = socket_strerror($errorcode);
    
            die("Could not connect: [$errorcode] $errormsg 
    ");
        }
    
    
        //Send the message to the server
        if (!socket_send($socket, $message, strlen($message), 0)) {
            $errorcode = socket_last_error();
            $errormsg = socket_strerror($errorcode);
    
            die("Could not send data: [$errorcode] $errormsg 
    ");
        }
    
        socket_close($socket);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?