1 回答
TA贡献1831条经验 获得超9个赞
终于找到了正确的密码,向我显示了包含此代码的页面
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 = '127.0.0.1';
$port = 15000;
//Implicit Flush
ob_implicit_flush();
// 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);
// Clients
$clients = [];
// Loop continuously
while (true) {
// Accept new connections
if ($newsock = socket_accept($sock)) {
if (is_resource($newsock)) {
// Set Non-block for the new connection
socket_set_nonblock($newsock);
// Append the new connection to the clients array
$clients[] = $newsock;
}
}
// Polling for new messages
if (count($clients)) {
foreach ($clients AS $k => $v) {
// Check for new messages
$string = '';
if ($char = socket_read($v, 1024)) {
$string .= $char;
}
// New string for a connection
if ($string) {
echo "$string <br>";
} else {
if ($seconds > 30) {
// Ping every 5 seconds for live connections
if (false === socket_write($v, 'PING')) {
// Close non-responsive connection
socket_close($clients[$k]);
// Remove from active connections array
unset($clients[$k]);
}
// Reset counter
$seconds = 0;
}
}
}
}
sleep(1);
$seconds++;
}
// Close the master sockets
socket_close($sock);
并完美地工作!
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报