I have a very simple server php code like this
function listenForClients()
{
$this->serviceConnection = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_bind($this->serviceConnection, "\tmp\mysock", 0);
socket_listen($this->serviceConnection, 10000000);
while($clientSocket = socket_accept($this->serviceConnection))
{
$clientMessage = socket_read($clientSocket, 1024);
socket_close($clientSocket);
}
}
Then I have a very simple client that does this
for ( $counter = 0; $counter <= 1000; $counter ++) {
$fp = fsockopen("unix///tmp/mysock", 0, $errno, $errstr);
if (!$fp){
echo "Error: Could not open socket connection at " . $counter . "\n";
exit;
}else{
fputs ($fp, "hello", strlen("hello"));
fclose($fp);
}
}
For some reason, after a random number of connections (around 300-500) fsockopen will return with a warning Resource temporarily unavailable. In the beginning I was getting the warning at around 20-30 connections. But once I increased the backlog parameter in socket_listen it got a bit better to around 300-500. How do I overcome this?
What is the way to build a php server socket to accept a lot of incoming connections per second (sustained).
Thanks!
The full error:
PHP Warning: fsockopen(): unable to connect to unix:///tmp/mysock:0 (Resource temporarily unavailable) in test.php on line 22