Footniko
Footniko

Reputation: 2752

Send data from php to c++ using tcp socket

I'm trying to send data from my simple php script to c++ programm, thad uses socket but I can't to reproduce this data correctly. My simple PHP script:

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

sleep(5);

fclose($motorPort);

My C++ code:

...
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]);
}
...

But I get: 48 49 48 48 Instead: 0 1 0 0

Upvotes: 0

Views: 205

Answers (1)

TecBrat
TecBrat

Reputation: 3719

You're sending ascii and expecting binary. http://www.ascii.cl/ ascii 48 0 ascii 49 1

I haven't done it myself, so I'm not sure of the details. But you probably need to use pack()

Upvotes: 1

Related Questions