PHP TCP socket server hangs when accepting a new connection - php

I'm trying to set up a TCP socket server, which should support many client connections at the same time, together with receiving and sending data.
For this purpose I'm trying to use PHP's socket_select(); due to server always hanged on socket_read(); process, where it should continue, no matter if there were data, or not. I Tried to run following code below, but it always hangs on a new client connection.
// TCP socket created
$clients = array($socket);
while (true) { // Infinite loop for server
$newsock = socket_accept($socket);
if ($newsock !== false) {
// Adds a new client
$clients[] = $newsock;
}
$read = $clients;
$write = NULL;
$except = NULL;
$num_changed_sockets = socket_select($read, $write, $except, 0);
if ($num_changed_sockets === false) {
// Error here
} else if ($num_changed_sockets > 0) {
// Something happened
if (in_array($socket, $read)) {
$key = array_search($socket, $read);
unset($read($key]);
}
foreach ($read as $read_socket) {
$data = socket_read($read_socket, 128); // This should not hang!
if ($data === false) {
// Disconnect client
}
// Reads data, sends answer...
}
}
// Something to send for all clients
}
Is it also possible to use socket_select(); without having a copy of my clients in an array, where the listener is included? Just having a clean array for clients.

Sockets are by default blocking, you should put the passive listening socket ($socket in your case) in the read set passed to socket_select as well, and it will be readable when you can accept new connections.

Just set the listening socket $socket to non-blocking mode. Clients get connected and data is being read, but the client disconnects are not handled in real time (there is a pretty big delay on it). Is there reason for that?

Related

stream_select timeout on newly opened socket

I use thrift php client to connect to java thrift server. But got the error of
TSocket: timed out writing 78 bytes from 10.0.1.151:1234
I dig into the php source of thrift and find it was caused from timeout on function stream_select.
/**
* Write to the socket.
*
* #param string $buf The data to write
*/
public function write($buf)
{
$null = null;
$write = array($this->handle_);
// keep writing until all the data has been written
while (TStringFuncFactory::create()->strlen($buf) > 0) {
// wait for stream to become available for writing
$writable = #stream_select($null, $write, $null, $this->sendTimeoutSec_, $this->sendTimeoutUsec_);
if ($writable > 0) {
// write buffer to stream
$written = #stream_socket_sendto($this->handle_, $buf);
if ($written === -1 || $written === false) {
throw new TTransportException('TSocket: Could not write '.TStringFuncFactory::create()->strlen($buf).' bytes '.
$this->host_.':'.$this->port_);
}
// determine how much of the buffer is left to write
$buf = TStringFuncFactory::create()->substr($buf, $written);
} elseif ($writable === 0) {
throw new TTransportException('TSocket: timed out writing '.TStringFuncFactory::create()->strlen($buf).' bytes from '.
$this->host_.':'.$this->port_);
} else {
throw new TTransportException('TSocket: Could not write '.TStringFuncFactory::create()->strlen($buf).' bytes '.
$this->host_.':'.$this->port_);
}
}
}
That means the socket was blocked and was not able to write. But the socket is just open and should not be blocked. I try select immediately after pfsockopen
if ($this->persist_) {
$this->handle_ = #pfsockopen($this->host_,
$this->port_,
$errno,
$errstr,
$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000));
} else {
$this->handle_ = #fsockopen($this->host_,
$this->port_,
$errno,
$errstr,
$this->sendTimeoutSec_ + ($this->sendTimeoutUsec_ / 1000000));
}
// Connect failed?
if ($this->handle_ === FALSE) {
$error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])';
if ($this->debug_) {
call_user_func($this->debugHandler_, $error);
}
throw new TException($error);
}
$write = array($this->handle_);
$writable = #stream_select($null, $write, $null, $this->sendTimeoutSec_, $this->sendTimeoutUsec_);
if ($writable === 0) {
die('123');
}
The result show that it is block right after it is opened!
When restart php-fpm, the error disappear for a while and come up again.
Here is the client code:
$socket = new TSocket('10.0.1.151', 1234, true);
$framedSocket = new TFramedTransport($socket);
$transport = $framedSocket;
$protocol = new TCompactProtocol($transport);
$transport->open();
$client= new userservice\UserServiceProxyClient($protocol);
$result = $client->findUser($id);
If I adjust the php-fpm configuration pm.max_children from 200 to 2, the error also disappear.
I tried increase timeout to 10 seconds and it timeout after 10 seconds.
Any idea for what's the cause of the problem and how to fix it?
I modify the php thrift lib. Change from
$writable = #stream_select($null, $write, $null, $this->sendTimeoutSec_, $this->sendTimeoutUsec_);
to
$writable = 1;
And the same for read.
The problem get fixed.
That means the connection is established ( I even use tcpdump to confirm the tcp connection is established) and writable, but select timeout. Strange things!

PHP detecting TCP client disconnect

I got a TCP server up and running, which supports many client connections at the same time, together with reading and sending data. However, when client closes the connection, there is a delay on the server to detect a disconnected client.
// TCP socket created here
$clients = array($socket);
while (true) {
$read = $clients; // Copy of $clients
$write = NULL;
$except = NULL;
$num_changed_sockets = socket_select($read, $write, $except, NULL);
if ($num_changed_sockets === false) {
// ...
} else if ($num_changed_sockets > 0) { // Something changed
printb("In here."); // EDIT
if (in_array($socket, $read)) { // For new connections and removing host from the $clients copy
$newsock = socket_accept($socket);
if ($newsock !== false) {
printb("Client $newsock connected");
$clients[] = $newsock;
}
$key = array_search($socket, $read);
unset($read[$key]);
}
foreach ($read as $read_socket) { // The actual problem starts here?
printb("Read part."); // EDIT
$data = socket_read($read_socket, 128);
if ($data === false) {
printb("Client $read_socket disconnected");
$key = array_search($read_socket, $clients);
socket_close($clients[$key]);
unset($clients[$key]);
continue;
}
}
}
EDIT I:
I just added debug prints in the code. When a single client disconnects, server falls into infinite loop from $num_changed_sockets. Is there way to fix it?
EDIT II:
var_dump gives empty string after a client has disconnected.
EDIT III:
According to last edit, disconnecting the client, if socket_read returns an empty string seems to work. I Found a note from PHP's website:
Note: socket_read() returns a zero length string ("") when there is no more data to read.
Should not this return false, when client disconnects?

socket_select never time out

I have been trying to make a PHP socket server, something I never done before. So I might not get how all the socket_* functions work.
What I have trouble with is the timeout function in socket_select.
while(true){
//Copy $clients so the list doesn't get modified by socket_select();
$read = $clients;
$write = $clients;
//new socket tries to connect
if(!$new = socket_accept($socket)){
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error()); break;
}
//Accept the new client
if(!in_array($new, $clients)){
$clients[] = $new;
sendMessage($new, "Hello and welcome to the PHP server!");
}
//Wait for read
socket_select($read, $write, $empty, 5, 5);
foreach($read as $client){
$id = array_search($client,$clients);
echo $id." ".readMessage($client);
}
//Write data to the connected sockets
foreach($write as $client){
sendMessage($client, rand(0,99999));
}
echo "I'm bored\n";
}
From what I understand of socket_select is that this script should say, "I'm bored" every 5 seconds. But it doesn't, why?
Why I want to timeout socket_select is to make a loop so I can send data to the connected sockets.
You're calling socket_accept() every time around the loop. This call will block if no new connections have arrived.
Add $socket to the array of sockets you pass to socket_select(), and only call socket_accept() if that socket shows up as readable. (You'll also need to make that socket an exception in your other loops so that you don't try to write to it.)

Event Listener in PHP

I want my web server to notify me through a php page when an event occurs at another TCP server, to which the PHP page has successfully connected via a socket. The event is like the TCP server wants to send a message to the web server, etc. Is there any way to accomplish this and/or any references on how to do it?
Sure:
$fp = fsockopen("tcp://example.com", 8888) OR die("could not connect");
while (!feof($fp)) {
$pc = fread($handle, 8192);
if ($pc === false || strlen($pc) == 0)
break;
//a new packet has arrived
//you should collect the read in a variable and wait
//for another packet until you know the message is complete
//(depends on the protocol)
collect_in_result($pc);
if (message_is_complete()) {
if (check_event()) {
//take action
}
}
}

Does my basic PHP Socket Server need optimization?

Like many people, I can do a lot of things with PHP. One problem I do face constantly is that other people can do it much cleaner, much more organized and much more structured. This also results in much faster execution times and much less bugs.
I just finished writing a basic PHP Socket Server (the real core), and am asking you if you can tell me what I should do different before I start expanding the core. I'm not asking about improvements such as encrypted data, authentication or multi-threading.
I'm more wondering about questions like "should I maybe do it in a more object oriented way (using PHP5)?", or "is the general structure of the way the script works good, or should some things be done different?". Basically, "is this how the core of a socket server should work?"
In fact, I think that if I just show you the code here many of you will immediately see room for improvements. Please be so kind to tell me. Thanks!
#!/usr/bin/php -q
<?
// config
$timelimit = 180; // amount of seconds the server should run for, 0 = run indefintely
$address = $_SERVER['SERVER_ADDR']; // the server's external IP
$port = 9000; // the port to listen on
$backlog = SOMAXCONN; // the maximum of backlog incoming connections that will be queued for processing
// configure custom PHP settings
error_reporting(1); // report all errors
ini_set('display_errors', 1); // display all errors
set_time_limit($timelimit); // timeout after x seconds
ob_implicit_flush(); // results in a flush operation after every output call
//create master IPv4 based TCP socket
if (!($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) die("Could not create master socket, error: ".socket_strerror(socket_last_error()));
// set socket options (local addresses can be reused)
if (!socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1)) die("Could not set socket options, error: ".socket_strerror(socket_last_error()));
// bind to socket server
if (!socket_bind($master, $address, $port)) die("Could not bind to socket server, error: ".socket_strerror(socket_last_error()));
// start listening
if (!socket_listen($master, $backlog)) die("Could not start listening to socket, error: ".socket_strerror(socket_last_error()));
//display startup information
echo "[".date('Y-m-d H:i:s')."] SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n"; //max connections is a kernel variable and can be adjusted with sysctl
echo "[".date('Y-m-d H:i:s')."] Listening on ".$address.":".$port.".\n";
$time = time(); //set startup timestamp
// init read sockets array
$read_sockets = array($master);
// continuously handle incoming socket messages, or close if time limit has been reached
while ((!$timelimit) or (time() - $time < $timelimit)) {
$changed_sockets = $read_sockets;
socket_select($changed_sockets, $write = null, $except = null, null);
foreach($changed_sockets as $socket) {
if ($socket == $master) {
if (($client = socket_accept($master)) < 0) {
echo "[".date('Y-m-d H:i:s')."] Socket_accept() failed, error: ".socket_strerror(socket_last_error())."\n";
continue;
} else {
array_push($read_sockets, $client);
echo "[".date('Y-m-d H:i:s')."] Client #".count($read_sockets)." connected (connections: ".count($read_sockets)."/".SOMAXCONN.")\n";
}
} else {
$data = #socket_read($socket, 1024, PHP_NORMAL_READ); //read a maximum of 1024 bytes until a new line has been sent
if ($data === false) { //the client disconnected
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
echo "[".date('Y-m-d H:i:s')."] Client #".($index-1)." disconnected (connections: ".count($read_sockets)."/".SOMAXCONN.")\n";
} else {
if ($data = trim($data)) { //remove whitespace and continue only if the message is not empty
switch ($data) {
case "exit": //close connection when exit command is given
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
echo "[".date('Y-m-d H:i:s')."] Client #".($index-1)." disconnected (connections: ".count($read_sockets)."/".SOMAXCONN.")\n";
break;
default: //for experimental purposes, write the given data back
socket_write($socket, "\n you wrote: ".$data);
}
}
}
}
}
}
socket_close($master); //close the socket
echo "[".date('Y-m-d H:i:s')."] SERVER CLOSED.\n";
?>
One small thing, personally i'd create a function for outputting instead of just using echo, that way its easy to turn it off, change the format etc.. eg
function log($message = '')
{
echo '['.date('Y-m-d H:i:s').']'.$message;
}
and then you can use :
log("SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n");
instead of
echo "[".date('Y-m-d H:i:s')."] SERVER CREATED (MAXCONN: ".SOMAXCONN.").\n";
Oh and be sure to use === instead of == otherwise you might get some odd results.
I'd move your switch $data into a function as that will likely expand.
Also since it looks like you're using a text based protocol, might want to explode/strtok to get the first level command and check it against an array of valid commands. Could also have an array that describes what internal function to call and use call_user_func_array to dispatch the call.

Categories