PHP Socket not sending command to client

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
agravayne
Forum Newbie
Posts: 8
Joined: Thu Dec 04, 2008 4:55 am

PHP Socket not sending command to client

Post by agravayne »

Hello,

I am trying out PHP sockets. I have succesfully got a socket server running, I can telnet into it and it behaves exactly as expected except one thing. You close the conecting by typing OK. What I am dong eventually is havinga client conect to the port then send its message, sending OK to end the session - but the server needs to send back a message to close the client.

This is my code adapted from PHP.nets example.

Code: Select all

 
<?php
error_reporting(E_ALL);
 
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
 
/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();
 
$address = '192.168.16.117';
$port = 9191;
 
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;
    }
    /* Send instructions. */
    $msg = "\nWelcome to the PHP Test Server. \n" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
    socket_write($msgsock, $msg, strlen($msg));
 
    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 ($buf == 'OK' || $buf=='ok') {
         $talkback = "pmvclose\r\n";
            socket_write($msgsock, $talkback, strlen($talkback));
         break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
 
    
        $talkback = "$buf\r\n";
        socket_write($msgsock, $talkback, strlen($talkback));
 
 
    
 
    
 
    } while (true);
    socket_close($msgsock);
} while (true);
    socket_close($sock);
?> 
 
 
The problem seems to be the following line

Code: Select all

if ($buf == 'OK' || $buf=='ok') {
         $talkback = "pmvclose\r\n";
            socket_write($msgsock, $talkback, strlen($talkback));
         break;
This should send the message pmvclose before closing the session but doesn't. Anyone got any ideas?

Many thanks
Scott
Post Reply