An old question, but for those who need to do this in the future ... The best (perhaps only) way would be to completely control the server as a server.
, 80 ( , ) , 80 .
, . , , , :
<?php
$port = (isset($argv[1])) ? intval($argv[1]) : 80;
function dlog($string) {
echo '[' . date('Y-m-d H:i:s') . '] ' . $string . "\n";
}
while (($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
dlog("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
sleep(1);
}
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
dlog("socket_set_option() failed: reason: " . socket_strerror(socket_last_error($sock)));
exit;
}
$tries = 0;
while (@socket_bind($sock, 0, $port) === false) {
dlog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)));
sleep(1);
$tries++;
if ($tries>30) {
dlog("socket_bind() failed 30 times giving up...");
exit;
}
}
while (@socket_listen($sock, 5) === false) {
dlog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)));
sleep(1);
}
socket_set_nonblock($sock);
$clients = array();
dlog("server started...");
while(true) {
while (($msgsock = @socket_accept($sock)) !== false) {
socket_set_nonblock($msgsock);
socket_getpeername($msgsock, $remote_address);
$clients[] = array('sock' => $msgsock, 'timeout' => time()+30, 'ip' => $remote_address);
dlog("$remote_address connected, client count: ".count($clients));
}
foreach($clients as $key => $client) {
$rec = '';
$buf = '';
while (true) {
$buf = socket_read($clients[$key]['sock'], 2048, PHP_BINARY_READ);
if ($buf === false) break;
$rec .= $buf;
if ($buf === '') break;
}
if ($rec=='') {
if ($clients[$key]['timeout']<time()) {
dlog('No data from ' . $clients[$key]['ip'] . ' for 30 seconds. Ending connection');
socket_close($client['sock']);
unset($clients[$key]);
}
} else {
$clients[$key]['timeout']=time()+30;
dlog('Raw data received from ' . $clients[$key]['ip'] . "\n------\n" . $rec . "\n------");
}
}
usleep(50000);
}
foreach($clients as $key => $client) {
socket_close($client['sock']);
}
@socket_close($sock);
exit;
8080, php filename.php 8080 .