stream_socket need help

Hi, can i ask some help,…How do i send command to the device using the stream_socket_client and getting the response?

Thank you in advance.

Can you be a little more clear in your request please? What device? Maybe supply the entire problem your trying to solve for?

Hi.Wolfe,

I want to send command to our device via stream_socket,but i am confuse to use this,all i want is to send command to device then i want to get the response from the device and write it to a file or log file.

here is my code,and it’s not working…


<?php
$command = "@@A27,013723008965451,D01,0*BB\\r\
";//this is the command i want to send to our device
$file = fopen('mylogs.log','w');
$server = stream_socket_server("tcp://127.0.0.1:1337", $errno, $errorMessage);

if ($server === false) {
    throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}


for (;;) {
    $socket = @stream_socket_accept($server);
    if ($socket) {
        stream_socket_sendto($socket, $command, STREAM_OOB);
        $dataflag =  stream_socket_recvfrom($socket, 1500, STREAM_OOB);
        stream_copy_to_stream($dataflag, $file);//copy bytess to mylogs.log
        fclose($file);
        fclose($socket);
    }
}


?>


I’m still unsure of what your “device” is. I’m assuming that your “device” already has a socket server present on it? Try to use telnet to establish a connection, and send that command. Once you get some connectivity and response, then let’s come back to this.

From the looks of your code, your trying to turn your localhost (not the remote device) into the socket server.

Hi K.Wolfe,

How do i test using telnet?..first i open cmd,then i typed telnet [enter],echo “test” 127.0.0.1 1337[enter]

it give me invalid

can you help me please

Thank you in advance.

Your trying to connect IN to the device correct? The ip address of your device is not going to be 127.0.0.1, that’s your localhost.

Hi,K.Wolfe,

can you please go in this thread http://www.sitepoint.com/forums/showthread.php?1212717-Need-help-for-the-response

I already tried telnet and it shows the command when i press enter…what does it mean?