Skip to content

Instantly share code, notes, and snippets.

@LinuxDoku
Created October 30, 2011 18:58
Show Gist options
  • Save LinuxDoku/1326280 to your computer and use it in GitHub Desktop.
Save LinuxDoku/1326280 to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ERROR);
set_time_limit(0);
// config
$host = 'localhost';
$port = 1414;
// connect
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $host, $port);
// listen
socket_listen($socket);
$sockets = array($socket);
$clients = array();
while(true) {
echo "Wait for connection...\r\n";
$sockets_change = $sockets;
$ready = socket_select($sockets_change, $write = null, $expect = null);
echo "Connected.\r\n";
foreach($sockets_change as $s) {
if($s == $socket) {
$client = socket_accept($socket);
array_push($sockets, $client);
print_r($sockets);
} else {
$bytes = @socket_recv($s, $buffer, 2048, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment