php socket fast way to send message to multiple clients? - php

I'm running a php server based on the one provided by a tutorial by Raymond Fain on kirupa:
http://www.kirupa.com/developer/flash8/php5sockets_flash8_3.htm
It works great, up to a point. The thing is that when it receives certain messages, it then does some stuff to that message then sends it out to all connected clients. The problem here is that once the number of clients reaches the atmospheric heights of around 12, the loop that sends the message to all clients can take a while (like 4 seconds), and any subsequent messages sent during that 4 second period get queued up and eventually we get timeouts.
This is the loop that sends a message to all clients:
function send_Message($allclient, $socket, $buf)
{
$now = microtime(true);
echo 'sending message to '.count($allclient).' clients ';
$msg = "<mbFeed>$buf</mbFeed>\n\0";
foreach($allclient as $client)
{
socket_write($client, $msg, strlen($msg));
}
$end = microtime(true);
echo 'time was '.($end - $now);
}
You'll notice I've been echoing the time it takes by comparing microtimes. The thing is that the echo claims that the whole process takes a tiny amount of time, like 0.003 seconds, which is what I would have expected, but when I have this script running in the terminal, I see the little spinning icon going for the four seconds, during which everything gets unresponsive.
My questions are as follows: does anyone know what the script is doing during that time? Is there anything I can do it to stop it doing that? Is there a more efficient way to send a message to all connected sockets?
Here's the code for the whole socket file, if you need it, but I'm hoping this is something that might be familiar to somebody...
#!/usr/bin/php -q
<?php
/*
Raymond Fain
Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com
For any questions or concerns, email me at ray#obi-graphics.com
or simply visit the site, www.php.net, to see if you can find an answer.
*/
//ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
//ini_set('error_log', 'socket_errors.log');
ini_set('log_errors', 'On');
ini_set('display_errors', '1');
error_log('testing');
stream_set_timeout(1,0);
set_time_limit(0);
ob_implicit_flush();
$address = 'xxx.xxx.x.xxx';
$port = xxxx;
function send_Message($allclient, $socket, $buf)
{
$now = microtime(true);
echo 'sending message to '.count($allclient).' clients ';
$msg = "<mbFeed>$buf</mbFeed>\n\0";
foreach($allclient as $client)
{
socket_write($client, $msg, strlen($msg));
}
$end = microtime(true);
echo 'time was '.($end - $now);
}
echo "connecting...
";
//---- Start Socket creation for PHP 5 Socket Server -------------------------------------
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0)
{
echo "socket_create() failed, reason: " . socket_strerror($master) . "\n";
}
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
socket_set_nonblock($master);
if (($ret = socket_bind($master, $address, $port)) < 0)
{
echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n";
}
echo 'socket bind successfull.
';
if (($ret = socket_listen($master, 5)) < 0)
{
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n";
}
$read_sockets = array($master);
echo "connected.";
//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true)
{
$changed_sockets = $read_sockets;
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
echo 'changed sockets length: '.(count($changed_sockets));
foreach($changed_sockets as $key => $socket)
{
if ($socket == $master)
{
if (($client = socket_accept($master)) < 0)
{
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
continue;
}
else
{
socket_set_nonblock($client);
array_push($read_sockets, $client);
}
}
else
{
$bytes = socket_recv($socket, $buffer, 8192, 0);
if ($bytes == 0)
{
unset($read_sockets[$key]);
unset($changed_sockets[$key]);
socket_close($socket);
}
else
{
if (substr($buffer, 0, 3) == "<->")
{
unset($read_sockets[$key]);
unset($changed_sockets[$key]);
socket_close($socket);
$buffer = substr($buffer, 3);
}
$allclients = $read_sockets;
array_shift($allclients);
if (substr($buffer, 0, 3) == ":::") handleSpecial(substr($buffer, 3));
else
{
echo 'allclients length: '.(count($allclients));
send_Message($allclients, $socket, str_replace("\0","",$buffer));
}
}
}
}
}
?>

I would like to recommend you to look at ZeroMQ its like sockets on steroids
ØMQ in a Hundred Words
ØMQ (also seen as ZeroMQ, 0MQ, zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fanout, pub-sub, task distribution, and request-reply. It's fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPLv3 open source.
using ZMQ::SOCKET_PUB and ZMQ::SOCKET_PULL the same chat server can be as simple as
$ctx = new ZMQContext();
$pub = $ctx->getSocket(ZMQ::SOCKET_PUB);
$pub->bind('tcp://*:5566');
$pull = $ctx->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://*:5567');
echo "Chat Server Start ", PHP_EOL;
while(true) {
$message = $pull->recv();
echo "Got ", $message, PHP_EOL;
$pub->send($message);
}
This can easily handle10,000 push request per min on a simple system as oppose to your current server implementation.
With ZmqSocket you can talk to zmq sockets from your JavaScript code. You can connect, send and receive string messages. As JavaScript does not support raw TCP connections, it uses Flash as a bridge
Simple JavaScript Bridge Example you can also look at zmqsocket-as which allows you to talk to zmq-sockets from ActionScript code.

Related

can receive Data After sending through php Sockets

i have a question about Sockets in PHP. My Environment Looks like follows:
one Server VM with Ubuntu 22.04 , and one VM with Windows 10. On The net is Global Cache IP2SL Adapter, on that interface is an Monitor with serial Line connected to this IP2SL.
The Goal should be , to send HEX Codes to the IP2SL which will be send to the Monitor. Some of These Codes have an Answer i.e. some Parameters like Brighness or Situation. In The end , on The Ubuntu is an Website where u can read and change those values . Therefore i've build some small testscripts to Check the Communication. To Monitor the line i use Wireshark, and both VM Linux and Windows run in a Parallels Environment on a Mac.
The Problem:
Did i send the hex code from windows to The IP2SL Adapter, anything looks fine, the command where send to the Monitor and the Monitor answer as expected. Did i send the same via php the command where send , and The Monitor Works with it as expected, if the commmand has no answer, i.e. PowerOn/Poweroff working like a charm, but if the Monitor should send an answer like brightness : XX% there come an "504 gateway TimeOut" After 60s on the Ubuntu System . did i do the same with IP2SL test App , the answer is correctly. In Wireshark is the Communication as expected. my php code Looks like follows
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$port = '4999';
$adr = "192.168.100.34";
$sendStr = array('E5', 'TARGET', '20', '86');//hexadecimal data
$target = "05";
function makeValue($v)
{
return pack('H*',dechex($v));
}
function toSend($adr,$port,$target,$cmd)
{
ob_implicit_flush();
// find target
for($i=0;$i < count($cmd);$i++)
{
if($cmd[$i] === "TARGET")
{
$cmd[$i] = hexdec($target);
}
else
{
$cmd[$i] = hexdec($cmd[$i]);
}
}
// build the CRC
$cmd[] = hexdec('FF') - array_sum($cmd) & hexdec('FF');
//var_dump($cmd);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//socket_set_nonblock($socket);
if ($socket === false) {
echo "socket_create() fehlgeschlagen: Grund: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "Socket erstellt OK.\r\n";
}
echo "Versuche, zu '$adr' auf Port '$port' zu verbinden ...";
$result = socket_connect($socket, $adr, $port);
if ($result === false) {
echo "socket_connect() fehlgeschlagen.\nGrund: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "Socket verbunden OK.\r\n";
}
$chars = array_map('makeValue',$cmd);
$out = join($chars);
$length = strlen($out);
//var_dump($out);
if(socket_write($socket, $out,$length))
{
usleep(25000);
$input = stream_get_contents($socket,1024);
socket_close($socket);
var_dump($input);
}
//socket_close($socket);
/*
if($input === FALSE || strcmp($input,'') == 0) {
$code = socket_last_error($socket);
socket_clear_error($socket);
socket_close($socket);
} else {
echo "Answer : ". bin2hex($input);
socket_close($socket);
}
*/
}
toSend($adr,$port,$target,$sendStr);
?>
Solution: The Solution is the array_map function 'makeValue'. The php pack command has an Format 'h*' and 'H*' , if the value is an Single Byte , the 'H*' converts it to the higher nibble , so the value '7' will Converted to '0x70'
function makeValue($v)
{
if(strlen($v)>1)
{
return pack('H*',dechex($v));
}
return pack('h*',dexhex($v));
}
this Solves the Problem

How to make websocket Read from a delphi application socket

I implemented Websockets in my server, it works great to communicate with the server itself, but it cannot connect directly do my Delhpi application, so I thought, "I'll just make Delphi send info to PHP, then make PHP push info to the client via Websocket!", GREAT! but it doesn't work.
I'm not sure why, but I think it has something to do with 2 sockets cannot be listening at the same time, so I think I need to use Threads or something like that.
On paper it looks easy, it looks like I just need to start a Thread with my function listening my Delphi application socket, and when it receives a message, I would send this to the parent process to push the message via websocket, but I cant help it but feel like there is something wrong with this tactic.
This is the code that reads from the socket of my Delphi app:
<?php
/* Cliente */
function enviarPct($skt, $pacote){
$msg = "$pacote\n";
socket_write($skt, $msg, strlen($msg));
echo "TX: $msg";
}
error_reporting(E_ALL);
echo "Conexao TCP/IP em PHP\n";
$address = "192.168.2.26";
$port = 63333;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $address, $port);
socket_set_option($sock, SOL_SOCKET, SO_KEEPALIVE, 1);
echo "Conexao: $address:$port\n\n";
enviarPct($sock,"login||20||");
//$msg = "login||20||\n";
//echo socket_write($sock, $msg, strlen($msg));
//echo socket_write($sock, utf8_encode($msg), mb_strlen($msg, 'utf-8'));
echo "Reading response:\n\n";
while ($out = socket_read($sock, 2048)) {
echo "RX: $out";
$pct = explode('||', $out);
if($pct[0]=='sucessoLogin'){
if($pct[1]!= '-1'){
enviarPct($sock, "solicitaControladoras||||");
}
}
}
//echo socket_read($sock, 2048);
echo "Fechando\n";
socket_close($sock);
?>
And Within the websocket aplication class, this funcion push messeges to the client, more specificaly that looping with "Im Waiting X Seconds"
/**
* Start a child process for pushing data
* #param unknown_type $client
*/
private function startProcess($client) {
$this->console("Start a client process");
$pid = pcntl_fork();
if($pid == -1) {
die('could not fork');
}
elseif($pid) { // process
$client->setPid($pid);
}
else {
// we are the child
while(true) {
// check if the client is connected
if(!$client->isConnected()){
break;
}
// push something to the client
$seconds = rand(2, 5);
$this->send($client, "I am waiting {$seconds} seconds");
sleep($seconds);
}
}
}
So thats it, if somebody have an idea of what would work best for me I would be glad to listen.

How to know whenever the connection is reset by peer in php?

I have been working lately on building a TCP server using PHP (I know wrong choice to begin with but this is the work standard), so I have reached a point where there is a reliable prototype to do tests on it and it showed good results. at start I used socket functions to handle to connection for server and it was working good but one of the main things on the project is to make the channel secured so I switched to stream_socket.
what I want is a socket_last_error equivalent in stream_socket group so I can know whenever the connection with client is closed or not. the current situation all processes will wait for timeout timer to release even tho the client is already closed.
I have searched the net and I found that there is no way to figure it out through PHP and I have found that some people opened issue ticket about it asking for socket_last_error equivalent for stream.
https://bugs.php.net/bug.php?id=34380
so is there anyway to know whenever FIN_WAIT signal is raised or not?
Thank you,
I don't think it's possible the stream_socket family, it looks like it's too high level.
I tried making a very hackish solution, I don't know if it will work for you, it's not very reliable:
<?php
set_error_handler('my_error_handler');
function my_error_handler($no,$str,$file,$line) {
throw new ErrorException($str,$no,0,$file,$line);
}
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)\n";
} else {
while ($conn = stream_socket_accept($socket)) {
foreach (str_split('The local time is ' . date('n/j/Y g:i a') . "\n") as $char) {
echo $char;
try {
fwrite($conn,$char);
} catch (ErrorException $e) {
if (preg_match("/^fwrite\(\): send of 1 bytes failed with errno=([0-9]+) ([A-Za-z \/]+)$/",$e->getMessage(), $matches)) {
list($errno,$errstr) = array((int) $matches[1], $matches[2]);
if ($errno === 32) {
echo "\n[ERROR] $errstr"; // Broken pipe
}
}
echo "\n[ERROR] Couldn't write more on $conn";
break;
}
fflush($conn);
}
fclose($conn);
}
fclose($socket);
}
echo "\n";
?>
Launch: php ./server.php
Connect: nc localhost 8000 | head -c1
Server output:
The loca
[ERROR] Broken pipe
[ERROR] Couldn't write more on Resource id #6

IRC related help

Now I have my bot to send message when the bot joins. However how do I make a form that would post data so that the bot will say the message to the channel?
Here is my script (Rewamped):
<?php
set_time_limit(0);
$socket = fsockopen("//", 6667) or die();
$msg = $_POST['message'];
$pr = $_POST['percentage'];
$pr /= 100;
fputs($socket,"USER BOT 0 zo :ZH bot\n");
// Set the bots nickname
fputs($socket,"NICK BOT1\n");
fputs($socket,"JOIN #bots\n");
while(1) {
while($data = fgets($socket, 128)) {
// echo the data received to page
echo nl2br($data);
// flush old data, it isn't needed any longer.
flush();
$ex = explode(' ', $data);
if($ex[0] == "PING") fputs($socket, "PONG ".$ex[1]."\n");
$search_string = "/^:([A-Za-z0-9_\-]+)[#!~a-zA-Z0-9#\.\-]+\s*([A-Z]+)\s*[:]*([\#a-zA-Z0-9\-]+)*\s*[:]*([!\#\-\.A-Za-z0-9 ]+)*/";
$do = preg_match($search_string, $data, $matches);
// check that there is a command received
if(isset($matches['2'])) {
switch($matches['2']) {
case "PRIVMSG":
$user = $matches['1'];
$channel = $matches['3'];
$chat_text = isset($matches['4']) ? $matches['4'] : "";
// check chat for !time
if(strtolower($chat_text) == "!time") {
$output = "::::: " . date('l jS \of F Y h:i:s A') . " :::::";
fputs($socket, "PRIVMSG " . $channel . " :" . $output . "\n");
} elseif(strtolower($chat_text) == "!hello") {
fputs($socket, "PRIVMSG " . $channel . " :Hello!\n");
}
break;
case "JOIN":
$user = $matches['1'];
$channel = $matches['3'];
fputs($socket, "PRIVMSG " . $channel . " :Welcome " . $user . " to " . $channel . "\n");
break;
}
}
}
}
?>
E.g. Making a form that would send the data to the IRC channel. The output would be "wget file info port" <-- That would be the text sent to the IRC channel.
Here are parts related:
fputs($socket, "PRIVMSG " . $channel . " :Welcome " . $user . " to " . $channel ."\n");
Hope someone can help out.
Okay here's a better answer. The first section still stands. A new PHP process is called every time you want to initiate a new script. Thus, you need some way to do IPC.
Here's how it's done on *nix (but not windows) in PHP:
Receiver:
<?php
$queueKey = 123321;
$queue = false;
if(msg_queue_exists($queueKey)) {
echo "Queue Exists.\n";
}
// Join the queue
$queue = msg_get_queue($queueKey);
while(!($queue == false)) {
// Note: This function could block if you feel like threading
$msgRec = msg_receive(
$queue, // I: Queue to get messages from
0, // I: Message type (0 = first on queue)
$msgType, // O: Type of message received
1024, // I: Max message size
$msgData, // O: Data in the message
true, // I: Unserialize data
MSG_IPC_NOWAIT // I: Don't block
);
if($msgRec) {
echo "Message received:\n";
echo "Type = $msgType\n";
echo "Data = \n";
print_r($msgData);
}
}
?>
Sender:
<?php
$queueKey = 123321;
$queue = false;
if(msg_queue_exists($queueKey)) {
echo "Queue Exists.\n";
} else {
echo "WARNING: Queue does not exist. Maybe no listeners?\n";
}
$queue = msg_get_queue($queueKey);
$abc["something"] = "something value";
$abc["hello"] = "world";
$abc["fu"] = "bar";
msg_send(
$queue, // Queue to send on
1, // Message type
$abc, // Data to send
true, // Serialize data?
true // Block
);
?>
This should produce (in the receiver loop) something similar to this:
Message received:
Type = 1
Data =
Array
(
[something] => something value
[hello] => world
[fu] => bar
)
Your script might look something like this
postToMe.php:
<?php
$queueKey = 123321;
$queue = false;
if(msg_queue_exists($queueKey)) {
echo "Queue Exists.\n";
} else {
echo "WARNING: Queue does not exist. Maybe no listeners?\n";
}
$queue = msg_get_queue($queueKey);
msg_send(
$queue, // Queue to send on
1, // Message type
$_POST, // Data to send
true, // Serialize data?
true // Block
);
?>
bot.php:
<?php
set_time_limit(0);
$socket = fsockopen("//", 6667) or die();
$msg = $_POST['message'];
$pr = $_POST['percentage'];
$pr /= 100;
fputs($socket,"USER BOT 0 zo :ZH bot\n");
// Set the bots nickname
fputs($socket,"NICK BOT1\n");
fputs($socket,"JOIN #bots\n");
$queueKey = 123321;
$queue = false;
// Join the IPC queue
$queue = msg_get_queue($queueKey);
if(!$queue) echo "ERROR: Could not join IPC queue. Form data will not be received";
while(1) {
// Handle new post info
// You may want to increase the message size from 1024 if post data is large
if(msg_receive($queue, 0, $msgType, 1024, $msgData, true, MSG_IPC_NOWAIT)) {
// Handle data here. Post data is stored in $msgData
}
while($data = fgets($socket, 128)) {
// echo the data received to page
echo nl2br($data);
// flush old data, it isn't needed any longer.
flush();
$ex = explode(' ', $data);
if($ex[0] == "PING") fputs($socket, "PONG ".$ex[1]."\n");
$search_string = "/^:([A-Za-z0-9_\-]+)[#!~a-zA-Z0-9#\.\-]+\s*([A-Z]+)\s*[:]*([\#a-zA-Z0-9\-]+)*\s*[:]*([!\#\-\.A-Za-z0-9 ]+)*/";
$do = preg_match($search_string, $data, $matches);
// check that there is a command received
if(isset($matches['2'])) {
switch($matches['2']) {
case "PRIVMSG":
$user = $matches['1'];
$channel = $matches['3'];
$chat_text = isset($matches['4']) ? $matches['4'] : "";
// check chat for !time
if(strtolower($chat_text) == "!time") {
$output = "::::: " . date('l jS \of F Y h:i:s A') . " :::::";
fputs($socket, "PRIVMSG " . $channel . " :" . $output . "\n");
} elseif(strtolower($chat_text) == "!hello") {
fputs($socket, "PRIVMSG " . $channel . " :Hello!\n");
}
break;
case "JOIN":
$user = $matches['1'];
$channel = $matches['3'];
fputs($socket, "PRIVMSG " . $channel . " :Welcome " . $user . " to " . $channel . "\n");
break;
}
}
}
}
?>
Basically, this script will be running all the time. The way PHP works is that for each script that is being run, a new PHP process is created. Scripts can be run multiple times simultaneously, however they will not be able to directly communicate.
You will need to create enother script (or at least a whole new function of this one) to accept the post variables, and then send them to the running version of this script.
(Note: I will provide 2 solutions, since 1 is significantly more difficult. Also, there's Semaphore that I've just found, however I am unsure exactly if this suits our needs because I know next to nothing about it http://php.net/manual/en/book.sem.php)
Best (But Advanced)
The best way I can think of doing this would be to use sockets (particularly on *nix, since sockets are fantastic for IPC [inter process communication]). It's a little difficult, since you're basically create a client/server just to communicate details, then you need to come up with some sort of a protocol for your IPC.
I won't code anything up here, but the links that are relevant to this are
http://www.php.net/manual/en/function.socket-create.php
http://www.php.net/manual/en/function.socket-bind.php
http://www.php.net/manual/en/function.socket-listen.php
http://www.php.net/manual/en/function.socket-accept.php
http://www.php.net/manual/en/function.socket-connect.php
If using this on *nix, I would highly recommend using AF_UNIX as the domain. It's very efficient, and quite a number of applications use it for IPC.
Pros:
Very robust solution
- Highly efficient
- Instant (or as close as we can get) communication
Cons:
- Quite difficult to implement
Not As Great (But Still Good)
Just use files to communicate the information. Have your bot script check the file every 15 seconds for changes. I would suggest using XML for the data (since simple xml makes xml processing in php well... simple)
Things you need to consider would be:
How would it react when receiving 2 posts at the same time? (If you just use a flat file or don't account for having multiple entries, this will become a problem).
How you find out if a message is new (I'd delete/blank the file right after reading. Note: Not after processing, as someone could post to the form script while you are processing/sending the message)
Links:
How to use simple xml
http://php.net/manual/en/simplexml.examples-basic.php
http://au2.php.net/manual/en/book.simplexml.php
File related
http://au2.php.net/manual/en/function.file-put-contents.php
http://au2.php.net/manual/en/function.file-get-contents.php
With that being said, you could also use MySQL/Postgres or some other database back end to deal with the flow of data between scripts.
Pros:
- Easy to implement
Cons:
- Slow to transfer data (checks files at given intervals)
- Uses external files, which can be deleted/modified my external applications/users

PHP Sockets, any way to speed this up

I'm writing a small script to see if certain ports on certain devices are in use or are open. My code is:
for($c=1;$c<=16;$c++){
echo "<tr><td>Pod " . $c . "</td>";
for ($d=5000;$d<=5010;$d++){
$tmp=fsockopen("10.0.0." . $c,$d,$erstr, $errno, 1);
if($tmp){
echo "<td class='o'>OPEN</td>";
fclose($tmp);
}
else{
echo "<td class='u'>IN USE</td>";
}
}
ob_flush();
flush();
echo "</tr>\n";
}
echo "</table>";
Obviously this is a lot of connections, and currently it's taking about ten seconds to run. I was wondering if there's any way I can get this to be a little bit faster? Thanks for the help!
One way to speed this up tremendously is to get asynchronous. Right now if one of the hosts is slow, it will slow down the entire pipeline because you're doing one right after another. PHP doesn't really have an event-based AIO (select), or even threading. It does, however, have fork in a linux environment. The following example hasn't been tested, but is a general idea for how to do asynchronous IO in php:
<?php
$childrenArr = array();
$childrenLeft = array();
for($c=1;$c<=16;$c++){
for ($d=5000;$d<=5010;$d++){
$pid = pcntl_fork();
if ($pid == -1) {
die("Could not fork");
} else if ($pid) {
$childrenArr[$pid] = array($c, $d);
$childrenLeft[$pid] = 1;
} else {
$tmp=fsockopen("10.0.0." . $c,$d,$erstr, $errno, 1);
exit(($tmp) ? 1 : 0);
}
}
}
$results = array();
while (count($childrenLeft) > 0) {
$oldPid = pcntl_waitpid(-1, $status, WNOHANG);
if (pcntl_wifexited($status )) {
unset($childrenLeft[$oldPid]);
list($c, $d) = $childrenArr[$oldPid];
$results[$c . "_" . $d] = pcntl_wexitstatus($status);
}
usleep(100);
}
for($c=1;$c<=16;$c++){
echo "<tr><td>Pod " . $c . "</td>";
for ($d=5000;$d<=5010;$d++){
if ($results[$c . "_" . $d]) {
echo "<td class='o'>OPEN</td>";
}
else {
echo "<td class='u'>IN USE</td>";
}
}
ob_flush();
flush();
echo "</tr>\n";
}
echo "</table>";
If a given port is not listening/accepting you will suffer all the TCP timeouts on the SYN packet re-transmissions sent out during the three-way handshake. This is the design of the TCP - we can't change that.
One thing I can recommend is switching from streams to the socket functions and trying non-blocking connect - create your 160 sockets, set them to non-blocking, initiate all connections, wait on them in select with a decreasing timeout, flag the ones that return readable. Do that in a loop until you spent, say, a whole second. Now you get a list of open TCP host/port pairs, and a list of likely closed ones, and you spent a fixed amount of time.
Disclaimer: I never do networking in PHP, so this might be totally useless.

Categories