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
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
After the accept you need to fork - see here http://php.net/manual/en/function.pcntl-fork.php
The accept needs to be in a loop. After accept fork a process to handle that client.
ie
while (TRUE)
{
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
socket_close($spawn);
} else {
// we are the child
// Use $spawn for communication as you see fit
// exit();
}
}
You will need to use signal handler to tidy up zombie processes.
The "brute" way is insert your code in a infinite loop.
While(1){
.
.
}
Other way, after received your message, you can recurseively call your script.
while( TRUE ) {
// your code
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.