Page 1 of 1

help with socket

Posted: Fri Feb 20, 2009 4:24 pm
by AluminX
Hello I'm fairly new to php development and i was trying to experiment with socket functions.
I got the general idea but still puzzled by some behaviors.
I'm working on windows xp.
below i have copied my code, file a.php(the server) and b.php(the client);
my understanding is this. when i run a.php(through php.exe) sock socket is created, binded, initialized to listen to porn 8080, enters the first do while loop and than just hangs there until a connection is established. when i call b.php, a sock socket is created, binded and connects to the specified ip on port 8080. Then it writes the request string and hangs for 10 seconds.
Meanwhile a.php opens a connection and creates a new socket assigning it to msgsock. The second do while loop is entered and $buf is assigned the first 1000 bytes read from $msgsock. The program continues executing the conditional statements until (echo "4) msg: $buf : From $addressRecive on $portRecive\n";) <- at this point is gets weird.
the port is some other number and not 8080. why is that?
anyway moving on. At this point the second do while should keep running until new data is is available right? It only runs twice and it hangs there until b.php resumes and makes the second request this time with send function instead of write(this was because i wanted to know the difference between the two, i still don't). Why would this hang there i assume that the loop should continuously running no?

To recap i need help with the following questions based on the code posted bellow.
1) why am i seeing a different port when i retrieve the information from socket_getpeername()?
2) why does the server(a.php) halts instead of running the do while loop when the client is halted as well?
3) what is the difference between socket_send() and socket_write() is it just the extra flag?

Thank you very much for your help

file a.php

Code: Select all

error_reporting(E_ALL);
 
/* Allow the script to hang around waiting for connections. */
echo "starting app...\r\n";
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.253.239';
$port = 9002;
 
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, null, $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 {
    echo "1) Executing do while...\r\n";
    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));
    echo "2) Executing do while $msgsock \r\n";
    do {
        echo "3)=Insite Do While \r\n";
        if (false === ($buf = socket_read($msgsock, 1000, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        echo "Reading buffer $buf\r\n";
        if (!$buf = trim($buf)) {
            continue;
            echo "continue \r\n";
        }
        if ($buf == 'quit') {
            break;
            echo "quit \r\n";
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
            echo "shutdown \r\n";
        }
        $talkback = "PHP: You said '$buf'.\n";
        //socket_write($msgsock, $talkback, strlen($talkback));
        $stuff = socket_getpeername($msgsock  , $addressRecive , $portRecive);
        echo "4) msg: $buf : From $addressRecive on $portRecive\n";
    } while (true);
    echo "5) **closing, '$msgsock'\r\n";
    socket_close($msgsock);
} while (true);
 
echo "ending";
socket_close($sock);
file b.php

Code: Select all

// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 
// An example list of IP addresses owned by the computer
$address    = '192.168.253.239';
 
// Bind the source address
socket_bind($sock, $address);
 
// Connect to destination address
socket_connect($sock, $address, 9002);
 
// Write
$request = "somethingelse\r\n";
/*socket_write($sock, $request);*/
socket_write ($sock, $request, strlen($request));
sleep(10);
$request = "**hello world**\r\n";
socket_send ($sock, $request, strlen($request) , MSG_DONTROUTE);
// Close
socket_close($sock);