codice:
<?
// don't timeout!
set_time_limit(0);
// set some variables
$host = "123.123.123.123";
$port = 10005;
// create socket
$socketS = Array();
$socketS[0] = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socketS[0], $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socketS[0], 3) or die("Could not set up socket listener\n");
// Array that will hold client information
$GO=1;
echo "Waiting for connections...\n";
$welcome = "welcome\n? ";
// keep looping and looking for client input
while ($GO>0) {
$connections=count($socketS);
for ($i = 0; $i < $connections; $i++){
if ($socketS[$i] != null)
$read[$i] = $socketS[$i];
}
$ready=socket_select($read,$null=null,$null=null,$null=null);
for ($i = 1; $i <$connections ; $i++){
if (in_array($socketS[$i] , $read)){
$input = socket_read($socketS[$i] , 1024);
echo "Form $i $input\n";
if ($input == null) {
echo "$i Disconnected\n";
socket_close($socketS[$i]);
array_splice($socketS ,$i,1);
if ($i!=$connections){
$connections--;};
}
}
}
if (in_array($socketS[0], $read)) {
$new_client = socket_accept($socketS[0]) or die("Could not accept incoming connection\n");
array_push($socketS, $new_client);
socket_write($new_client, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
socket_getpeername($new_client,$Host_URL,$Host_PORT);
echo "\n Received connection request from : $Host_URL:$Host_PORT\n";
}
}
// close primary socket
socket_close($socketS[0]);
echo "Socket terminated\n";
?>