phpconsoletcpfsockopen

Best way to execute commands to TCP/IP console from PHP


Before I start, excuse my english, I'm from Holland :)

I have a question regarding the use of PHP's fsockopen.

My Prerequisites

So basically, I have a Windows program running in the background which has a remote console over TCP/IP that I need to connect to so I can execute a few commands. I am able to connect to that console with KiTTY, and execute my commands without any problems.

My Solution

So the issue I have right now, is that I need to be able to execute these commands from the browser. I have searched the interwebs for best ways to do this and what I found was to use PHP's fsockopen to connect to my console. The code I tried is as follows:

$SOCKET = fsockopen("127.0.0.1", 12101, $errno, $errstr);

if($SOCKET){
 echo "Connected!";
}

$firstRead = fread($SOCKET, 8000);

echo($firstRead);

And using fputs to send a command:

fputs($SOCKET, "HELP \r\n");

And after, reading out my response with this:

$response = fread($SOCKET, 8000);
echo $response;

The Problem(s)

But I have encountered a few weird problems when testing this. As soon as I execute a command like "HELP", I can see from my KiTTY session that the command was executed and that I got a response, but when I read out the response with "fread" I get nothing. But when I use a loop to read it out like this, it reads something from the console at the second try almost everytime:

do {
    $response = fread($SOCKET, 8000);
    $i++;
} while (strlen($response) < 5 || $i < 5);

( Sometimes, it DOES read something from console on first try, but mostly it only reads something on second try ).

The Question

Now my question(s) is(are), why does it behave so strangely? And is it because I am doing something wrong? And is this really the best way to do this?

sidenote

When this works, I need to be able to call these PHP functions ( or something similar ) with a bunch of AJAX requests and get the response to show in the browser. This is an absolute MUST so please keep this in mind when writing a possible answer :)

Thanks everyone!


Solution

  • When you create a socket with fsockopen in PHP you might also want to specify if it is blocking or non-blocking, in case of a non-blocking socket the function socket_read will return false on error or if the connection was closed, or empty string until some data is received, in case of a blocking socket instead when you read on it, it will block until there is some data to read (or empty string if a timeout is hit). The behavior you described seems to be non-blocking.

    For changing the blocking type there are: socket_set_block and socket_set_nonblock.

    When your code with sockets works, there won't be any problems with AJAX requests, but keep in mind to set a timeout in PHP socket, otherwise if the server is down or simply too slow the request will fail with error (a timeout from php if set_time_limit is exceeded, which is a fatal error, or a JavaScript one with the browser timeout constant).

    Here are the links to manual of socket_read and socket_write, which I think are more appropriated of fread and fputs.