gpt4 book ai didi

PHP socket_recv 十六进制为 20 字节但打印为空

转载 作者:可可西里 更新时间:2023-11-01 02:54:35 25 4
gpt4 key购买 nike

我正在尝试与秤通信并发送数据。我正在使用 wireshark 来捕获所有数据。在 wireshark 上,当我发送我的代码时,您可以看到返回数据下方的比例,但我无法使用 socket_recv 在 php 上返回相同的数据。

Wire shark 收到的数据:10:01:00:00:00:00:00:14:01:00:00:00:00:01:00:01:00:00:00:00

我的 php 代码结果:Socket created Connection established Message send successful 20($buf 总字节数)如您所见,$buf 不是空的,它只是不打印它。我的php代码

<?php
$address = "192.168.15.51";
$port = "8071";
$buf='';

if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

//Connect socket to remote server
if(!socket_connect($sock , $address , $port))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not connect: [$errorcode] $errormsg \n");
}

echo "Connection established \n";

$message = "\x10\x01\x00\x00\x00\x00\x00\xf2";
$message2 = "\x10\x01\x00\x00\x00\x00\x00\x00\x00\xe01 1,1,1,0,0,2,0,1000,0,2,0,1,,,,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,,,,,,2,,,0,0,2,1,0,0,0,0,0,0,1,0,3,0,0,0,0,,,0,\"\\x0d\\x0dtest1\",0,800,,0,0,,0,0,0,0,0,0,,,,,0,0,0,0,1,0,,0,0,0,0,0,0,0,0,0,1,,0,,,,0,0,,,,0,0,0,0,0,\x10\x01\x00\x00\x00\x00\x00\x00\x00\xe0 2,2,2,0,0,2,0,1000,0,2,0,1,,,,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,,,,,,2,,,0,0,2,1,0,0,0,0,0,0,1,0,3,0,0,0,0,,,0,\"\\x0d\\x0dplu2\",0,100,,0,0,,0,0,0,0,0,0,,,,,0,0,0,0,1,0,,0,0,0,0,0,0,0,0,0,1,,0,,,,0,0,,,,0,0,0,0,0,";

//Send the message to the server
if( ! socket_send ( $sock , $message , strlen($message) , 0))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not send data: [$errorcode] $errormsg \n");
}
if( ! socket_send ( $sock , $message2 , strlen($message2) , 0))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not send data: [$errorcode] $errormsg \n");
}
echo "Message send successfully \n";

//Now receive reply from server
if(($byte=socket_recv ( $sock , $buf , 2045 , MSG_WAITALL )) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);

die("Could not receive data: [$errorcode] $errormsg \n");
}

//print the received message
socket_close($sock);
echo $byte . "\n";


echo $buf . "\n";

最佳答案

您永远不会初始化 $buf 变量。

将以下行放在 socket_recv 调用之前的某处。

$buf = "";

关于PHP socket_recv 十六进制为 20 字节但打印为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22673580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com