How can I make my server.php run continually?

client, php, sockets

Solution

I figured it out. Most of you were close to solving it like I was by using while() loop. But you cannot just put your code inside the while and expect it to work. The right way of doing it is as follows:

<?php 
$host = "127.0.0.1"; 
$port = 1234; 

// don't timeout! 
set_time_limit(0); 

// create socket 
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); 

// bind socket to port 
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); 
    while(true) {
    // start listening for connections 
    $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); 

    // accept incoming connections 
    // spawn another socket to handle communication 
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); 

    // read client input 
    $input = socket_read($spawn, 1024) or die("Could not read input\n"); 

    // clean up input string 
    $input = trim($input); 

    // reverse client input and send back 
    $output = strrev($input) . "\n"; 
    socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); 
    }
// close sockets 
socket_close($spawn); 
socket_close($socket); 

?>

If you try to put the while any other place it will introduce an error. Thanks everyone for help :D

Problem

So I have this server code and it works with my client. But it gets one message from the client and sends a message back reversed. Here is the code: SERVER.php ``` <?php $host = "127.0.0.1"; $port = 1234; // don't timeout! set_time_limit(0); // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input $input = socket_read($spawn, 1024) or die("Could not read input\n"); // clean up input string $input = trim($input); // reverse client input and send back $output = strrev($input) . "\n"; socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); // close sockets socket_close($spawn); socket_close($socket); ?> ``` How can I edit this code so it can run continually? The client does not have to stay up of course, it will just open a new socket, send a message, get it back from server and close the socket. Next time I want to send a message, I will do the previous step again. Now if i send a message and get a respond from server, they both close the socket. Please help me to modify the server side so that it will not close the socket and wait for a new connection. I tried to add a while loop but as soon the client closes, the server closes again saying that it could not read from client anymore. Thanks

Original source