Jump to content

do while (true)


Nev

Recommended Posts

Hi all, I need a little help please. I've got some code that creates a socket and a echo server, but I'm not having any luck in getting it to run. I've got the socket created, and closes, but when I try to run the code inside the "Do" "While" section the web page just hangs. I can see the port created on my server. Can some please shed some light at to what I'm doing wrong or what settings I need to change. My server is Apache/2.0.64 (Win32) PHP/5.2.17  .code below. Thanks in anticipation. Nev

 

<?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.0.7';
$port = 10001;
 
 
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason:1 " . socket_strerror(socket_last_error()) . "\n";
}
 
if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason:2 $address" . socket_strerror(socket_last_error($sock)) . "\n";
}
echo '<p>Hello World</p>';
 
if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason:3 " . socket_strerror(socket_last_error($sock)) . "\n";
}
 
 
 
do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason:4 " . 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 == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);
 
 
socket_close($sock);
?>
 
Link to comment
Share on other sites

Thanks for your reply, I'm a bit new to this and I was wondering if you could explain "cli". Currently I'm trying to run from my laptop's web browser, through my local intranet. How should I run this program.

Thanks again.

Link to comment
Share on other sites

Cli as in Client, I guess.

 

And I wouldn't have a do {} while(true) running all the time, initialize a variable that holds the value true and change it during function execution so that you have a way to fall out of the loop.

Link to comment
Share on other sites

Cool, thanks for the tip. I've managed to get this to work now, I had to open the port on my server pc and my laptop. Now I can telenet into the server. When I type "shutdown" it it stops the server, if I type quit it disconnects but does not shut down the server, so unable to start as already running.  

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.