0

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

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! You are right! For my case I use pack("c*", 0,1,0,0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.