note 105735 added to ref.sockets
| From: | paul dot hodel at gmail dot com | Date: | Sat, 10 Sep 2011 23:39:37 +0000 |
| Subject: | note 105735 added to ref.sockets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183089@lists.php.net to get a copy of this message | ||
After many non-sleep nights I got the most simple multi-client server written in PHP that really
works. Ctrl+C and Ctrl+V... use as command line to test it. Enjoy it.
<?php
ini_set('error_reporting', E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '10.203.9.67';
$port = 6901;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
// Non block socket type
socket_set_nonblock($sock);
// Loop continuously
while (true)
{
unset($read);
$j = 0;
if (count($client))
{
foreach ($client AS $k => $v)
{
$read[$j] = $v;
$j++;
}
}
$client = $read;
if ($newsock = @socket_accept($sock))
{
if (is_resource($newsock))
{
socket_write($newsock, "$j>", 2).chr(0);
echo "New client connected $j";
$client[$j] = $newsock;
$j++;
}
}
if (count($client))
{
foreach ($client AS $k => $v)
{
if (@socket_recv($v, $string, 1024, MSG_DONTWAIT) === 0)
{
unset($client[$k]);
socket_close($v);
}
else
{
if ($string)
{
echo "$k: $string\n";
}
}
}
}
//echo ".";
sleep(1);
}
// Close the master sockets
socket_close($sock);
----
Server IP: 187.73.33.5
Probable Submitter: 189.27.1.206
----
Manual Page -- http://www.php.net/manual/en/ref.sockets.php
Edit -- https://master.php.net/note/edit/105735
Del: integrated -- https://master.php.net/note/delete/105735/integrated
Del: useless -- https://master.php.net/note/delete/105735/useless
Del: bad code -- https://master.php.net/note/delete/105735/bad+code
Del: spam -- https://master.php.net/note/delete/105735/spam
Del: non-english -- https://master.php.net/note/delete/105735/non-english
Del: in docs -- https://master.php.net/note/delete/105735/in+docs
Del: other reasons-- https://master.php.net/note/delete/105735
Reject -- https://master.php.net/note/reject/105735
Search -- https://master.php.net/manage/user-notes.php