Skip to content

Instantly share code, notes, and snippets.

@nalkat
Last active July 3, 2018 12:59
Show Gist options
  • Save nalkat/2f8a0b1dd588744e064b90d27ba9c03d to your computer and use it in GitHub Desktop.
Save nalkat/2f8a0b1dd588744e064b90d27ba9c03d to your computer and use it in GitHub Desktop.
This is a simple network port server which allows multiple clients to connect, however it does not handle concurrent access.... If anyone has ideas on how to solve it, please let me know. pcntl_fork() on clients did not seem to bear fruit.
<?php // 7.3.0-dev
$clientPort;
$clientIP;
$address = "127.0.0.1";
$port = 9001;
if (($listenSocket = @socket_create(AF_INET, SOCK_STREAM, 0)) === false) exit ("could not create socket");
if ((@socket_set_option($listenSocket, SOL_SOCKET, SO_REUSEADDR, 1)) === false) exit ("could not set the socket to be reusable");
if ((@socket_bind($listenSocket, $address, $port)) === false) exit ("could not bind the socket to the address:port provided");
if ((@socket_listen ($listenSocket)) === false) exit ("could not listen to socket");
if ((@socket_set_nonblock ($listenSocket)) === false) exit("could not set the socket to non-blocking mode");
$clientArray = array();
$clientArray[] = $listenSocket;
$clientSockets = array();
$writers = array();
$exceptions = array();
$isListening = true;
$prompt = "input something: ";
do {
// use a copy of the array to select through current clients..
$clientSockets = $clientArray;
if (($selected = @socket_select($clientSockets, $writers, $exceptions, 0, 0)) === false) {
break;
} else {
foreach ($clientSockets as $client) {
if ($client === $listenSocket) { // new connection
if (($client = socket_accept ($listenSocket)) === false) {
break 2;
} else { // add new client
$clientArray[] = $client;
$key = array_search($client,$clientArray);
socket_getpeername($client, $clientIP, $clientPort);
socket_write($client, "Welcome ... ($clientIP : $clientPort)" . PHP_EOL);
socket_write($client, $prompt);
}
} else { // already connected
$key = array_search($client,$clientArray);
if (($msg = socket_read($client, 1024, PHP_BINARY_READ)) === false) {
if ($msg != '') { // unexpected disconnect happened
socket_close($client);
unset($clientArray[$key]);
break;
} else { // end of client input (e.g. they hit enter or stopped transmitting)
socket_close($client);
unset($clientArray[$key]);
}
} else {
// clean up the client input
$msg = filter_var($msg,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_BACKTICK|FILTER_FLAG_STRIP_HIGH);
if ($msg === '') {
continue;
}
// the message did not contain an EOL mark -- probably a netcat client
if (strchr ($msg,"\n") === false) {
unset($clientArray[$key]); // disconnect
} else {
// clean the message even more
$msg = filter_var($msg,FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_BACKTICK|FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
$msg = preg_replace('/[^[:alnum:][:space:]\!\?_\-\r\n]/u','',$msg);
// do something with the input the user gave you....
/* processInput($client,$msg); */ // you have to write this stuff here to handle input from the user
socket_write($client, "the message you gave me was: $msg" . PHP_EOL);
}
// try to connect multiple clients ... it will block here because there is no fork()
sleep(20);
}
socket_write($client,$prompt);
}
} // end of current client, on to the next ...
}
} while ($isListening === true);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment