我正在使用PHP套接字在TCP / IP级别上接收数据,我能够使用当前代码来执行此操作,但是似乎在一段时间后套接字服务器崩溃并且端口变得无用。
发生这种情况时,我无法再通过此端口接收数据,并且我接收的数据来自设备,这些设备分布在多个位置。然后,我无法手动更改端口以使其重新启动并运行。
我想知道是否有一种方法可以结束进程/解除阻塞端口并在崩溃时重新启动进程?
<?php
require_once("SocketServer.php"); // Include the File
$server = new SocketServer("XX.XXX.XX.XX",3030); // Create a Server binding to the given ip address and listen to port XXXX for connections
$server->max_clients = 10; // Allow no more than 10 people to connect at a time
$server->hook("CONNECT","handle_connect"); // Run handle_connect every time someone connects
$server->hook("INPUT","handle_input"); // Run handle_input whenever text is sent to the server
$server->infinite_loop(); // Run Server Code Until Process is terminated.
function handle_connect(&$server,&$client,$input)
{
SocketServer::socket_write_smart($client->socket,"String? ","");
}
function handle_input(&$server,&$client,$input)
{
// You probably want to sanitize your inputs here
$trim = trim($input); // Trim the input, Remove Line Endings and Extra Whitespace.
$file_p = dirname(__FILE__) . '/input.json';
$fp = fopen($file_p,"wb");
fwrite($fp,$trim);
fclose($fp);
if(strtolower($trim) == "quit") // User Wants to quit the server
{
SocketServer::socket_write_smart($client->socket,"Oh... Goodbye..."); // Give the user a sad goodbye message, meany!
$server->disconnect($client->server_clients_index); // Disconnect this client.
return; // Ends the function
}
SocketServer::socket_write_smart($client->socket,$output); // Send the Client back the String
SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String
}