编辑/更新2017年8月16日:
用户和库作者@Navarr评论说,他发布了该库的一个新的更新版本,我的原始答案中的代码基于该库。他的github上的新代码的链接在这里。随意探索新代码,并在此处返回原始示例以获取见识(我个人没有探索/使用新代码)。
以下代码将使用此处找到的SocketServer.class.php文件。它打算作为一个独立的进程运行,这意味着在Linux下,我必须使文件可执行,然后使用“
php
my_server.php”从命令行运行它。有关从命令行运行php脚本的更多信息:http
://www.funphp.com/?p =33
首先在此处获取SocketServer.class.php文件:http
://www.phpclasses.org/browse/file/31975.html
尝试使用它,然后进行调整以根据需要处理接收自己的传入数据。希望能帮助到你。
<?phprequire_once("SocketServer.class.php"); // Include the File$server = new SocketServer("192.168.1.6",31337); // Create a Server binding to the given ip address and listen to port 31337 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. 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 } $output = strrev($trim); // Reverse the String SocketServer::socket_write_smart($client->socket,$output); // Send the Client back the String SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String}编辑:为了使此答案具有相关性和功能性,我发现最好不要继续依赖可能并非始终可用的外部源代码(或通过我的链接中提供的给定URL)。因此,为了方便起见,我在下面添加了与我在本文顶部链接到的SocketServer.class.php文件相对应的代码。(为长度和复制/粘贴时可能缺少缩进/格式而致歉,我不是下面代码的作者)。
<?php class SocketServer { protected $config; protected $hooks; protected $master_socket; public $max_clients = 10; public $max_read = 1024; public $clients; public function __construct($bind_ip,$port) { set_time_limit(0); $this->hooks = array(); $this->config["ip"] = $bind_ip; $this->config["port"] = $port; $this->master_socket = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($this->master_socket,$this->config["ip"],$this->config["port"]) or die("Issue Binding"); socket_getsockname($this->master_socket,$bind_ip,$port); socket_listen($this->master_socket); SocketServer::debug("Listenting for connections on {$bind_ip}:{$port}"); } public function hook($command,$function) { $command = strtoupper($command); if(!isset($this->hooks[$command])) { $this->hooks[$command] = array(); } $k = array_search($function,$this->hooks[$command]); if($k === FALSE) { $this->hooks[$command][] = $function; } } public function unhook($command = NULL,$function) { $command = strtoupper($command); if($command !== NULL) { $k = array_search($function,$this->hooks[$command]); if($k !== FALSE) { unset($this->hooks[$command][$k]); } } else { $k = array_search($this->user_funcs,$function); if($k !== FALSE) { unset($this->user_funcs[$k]); } } } public function loop_once() { // Setup Clients Listen Socket For Reading $read[0] = $this->master_socket; for($i = 0; $i < $this->max_clients; $i++) { if(isset($this->clients[$i])) { $read[$i + 1] = $this->clients[$i]->socket; } } // Set up a blocking call to socket_select if(socket_select($read,$write = NULL, $except = NULL, $tv_sec = 5) < 1) { // SocketServer::debug("Problem blocking socket_select?"); return true; } // Handle new Connections if(in_array($this->master_socket, $read)) { for($i = 0; $i < $this->max_clients; $i++) { if(empty($this->clients[$i])) { $temp_sock = $this->master_socket; $this->clients[$i] = new SocketServerClient($this->master_socket,$i); $this->trigger_hooks("CONNECT",$this->clients[$i],""); break; } elseif($i == ($this->max_clients-1)) { SocketServer::debug("Too many clients... :( "); } } } // Handle Input for($i = 0; $i < $this->max_clients; $i++) // for each client { if(isset($this->clients[$i])) { if(in_array($this->clients[$i]->socket, $read)) { $input = socket_read($this->clients[$i]->socket, $this->max_read); if($input == null) { $this->disconnect($i); } else { SocketServer::debug("{$i}@{$this->clients[$i]->ip} --> {$input}"); $this->trigger_hooks("INPUT",$this->clients[$i],$input); } } } } return true; } public function disconnect($client_index,$message = "") { $i = $client_index; SocketServer::debug("Client {$i} from {$this->clients[$i]->ip} Disconnecting"); $this->trigger_hooks("DISCONNECT",$this->clients[$i],$message); $this->clients[$i]->destroy(); unset($this->clients[$i]); } public function trigger_hooks($command,&$client,$input) { if(isset($this->hooks[$command])) { foreach($this->hooks[$command] as $function) { SocketServer::debug("Triggering Hook '{$function}' for '{$command}'"); $continue = call_user_func($function,$this,$client,$input); if($continue === FALSE) { break; } } } } public function infinite_loop() { $test = true; do { $test = $this->loop_once(); } while($test); } public static function debug($text) { echo("{$text}rn"); } public static function socket_write_smart(&$sock,$string,$crlf = "rn") { SocketServer::debug("<-- {$string}"); if($crlf) { $string = "{$string}{$crlf}"; } return socket_write($sock,$string,strlen($string)); } function &__get($name) { return $this->{$name}; } } class SocketServerClient { protected $socket; protected $ip; protected $hostname; protected $server_clients_index; public function __construct(&$socket,$i) { $this->server_clients_index = $i; $this->socket = socket_accept($socket) or die("Failed to Accept"); SocketServer::debug("New Client Connected"); socket_getpeername($this->socket,$ip); $this->ip = $ip; } public function lookup_hostname() { $this->hostname = gethostbyaddr($this->ip); return $this->hostname; } public function destroy() { socket_close($this->socket); } function &__get($name) { return $this->{$name}; } function __isset($name) { return isset($this->{$name}); }}


