【问题标题】:Send data from php to c++ using tcp socket使用 tcp 套接字将数据从 php 发送到 c++
【发布时间】:2014-09-06 04:25:51
【问题描述】:

我正在尝试将数据从我的简单 php 脚本发送到 c++ 程序,使用套接字,但我无法正确重现这些数据。 我的简单 PHP 脚本:

$motorPort = fsockopen("192.168.2.1", 4444, $errno, $errstr, 30);
fwrite($motorPort, '0100');

sleep(5);

fclose($motorPort);

我的 C++ 代码:

...
unsigned char buffer[BUFFER_SIZE];
...
ssize_t count = recv(client_handle, &buffer, BUFFER_SIZE, 0);
...
unsigned int cmd;
unsigned long commands_count = count/4;
for(cmd = 0; cmd != commands_count; ++cmd)
{
    printf("Buffer = %d\n", buffer[0]);
    printf("Buffer = %d\n", buffer[1]);
    printf("Buffer = %d\n", buffer[2]);
    printf("Buffer = %d\n", buffer[3]);
}
...

但我得到: 48 49 48 48 反而: 0 1 0 0

【问题讨论】:

    标签: php c++ sockets


    【解决方案1】:

    您正在发送 ascii 并期待二进制文件。 http://www.ascii.cl/ascii 48 0ascii 49 1

    我自己没有做过,所以我不确定细节。但是你可能需要使用pack()

    【讨论】:

    • 是的!你说的对!对于我的情况,我使用 pack("c*", 0,1,0,0)
    猜你喜欢
    • 2022-11-12
    • 2014-10-05
    • 2013-07-04
    • 2020-07-13
    • 1970-01-01
    • 2021-10-23
    • 2016-05-09
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多