LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP - can't get to sockets to communicate (https://www.linuxquestions.org/questions/programming-9/php-cant-get-to-sockets-to-communicate-597761/)

ivanatora 11-07-2007 08:34 AM

PHP - can't get to sockets to communicate
 
I'm using the following server.php (most of it is taken directly from PHP docs):
Code:

<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 10000;
$pass = "qazwsx";
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
        echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
        echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
        if (($msgsock = socket_accept($sock)) === false) {
                echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
                break;
        }
        do {
                if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
                        echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
                        break 2;
                }
                if (!$buf = trim($buf)) {
                        continue;
                }
                if (preg_match("/^$pass/",$buf)) {
                        $cmd = substr($buf,strlen($pass));
                        $out = `$cmd`;
                        socket_write($msgsock, $out, strlen($out));
                }
        } while (true);
        socket_close($msgsock);
} while (true);
socket_close($sock);
?>

And the following client.php:
Code:

<?php
error_reporting(E_ALL);

$service_port = 10000;

$address = '127.0.0.1';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
        echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
        echo "OK.\n";
}

$out = '';
$in = "qazwsxdf -h";
echo "Sending request...";
$er = socket_write($socket, $in, strlen($in));
if ($er == 0){
        print socket_strerror(socket_last_error($socket));
}
print "ER: $er\n";
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
        echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>

However, the socket connects, and I'm getting only this message from the client:
Code:

OK.
Attempting to connect to '127.0.0.1' on port '10000'...OK.
Sending HEAD request...ER: 11
OK.
Reading response:

And it stays this forever. No talkback is done from the server. Actually it seems it doesn't recieve any input at all.

If I try to connect via telnet, and write the same string "qazwsxdf -h" everything is correct.
What am I doing wrong?

bigearsbilly 11-07-2007 08:56 AM

you will need to terminate your line.
an inet socket is (generally)
line buffered.
so the server is waiting for the rest of the line or a terminator (T one thousand, liquid metal)

try appending a '\n' (or possibly a \r\n pair )

ivanatora 11-08-2007 05:54 AM

You are 100% right.
I've added \n to the end of every line, and now they are proceeded correctly.
1 level up in life buffering :)

bigearsbilly 11-08-2007 06:54 AM

Quote:

Originally Posted by ivanatora (Post 2951978)
You are 100% right.

blimey, there's a first time for everything
;)


All times are GMT -5. The time now is 04:49 AM.