due to PHP standars modify the code to:
<?php
//...
$write = NULL;
$except = NULL;
$tv_sec = 5;
// Set up a blocking call to socket_select
if(socket_select($read, $write, $except, $tv_sec) < 1)
//...
?>
Example #1 Socket举例:简单的TCP/IP服务器
这个例子展示了一个简单的对讲服务器。改变地址和端口以满足你的设置和执行。 你可以使用类似于这样的命令:telnet 192.168.1.53 10000连接到服务器,(这里是你设置的地址和端口)。 你输入任何东西都会在服务器端输出来,然后回显给你。断开连接,请输入'quit'。
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
Example #2 Socket举例:简单的TCP/IP客户端
这个例子展示了一个简单的,一次性的HTTP客户端。 它只是连接到一个页面,提交一个HEAD请求,输出回复,然后退出。
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
due to PHP standars modify the code to:
<?php
//...
$write = NULL;
$except = NULL;
$tv_sec = 5;
// Set up a blocking call to socket_select
if(socket_select($read, $write, $except, $tv_sec) < 1)
//...
?>
A more object oriented example of the server
#!/usr/bin/env php
<?php
class MySocketServer
{
protected $socket;
protected $clients = [];
protected $changed;
function __construct($host = 'localhost', $port = 9000)
{
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//bind socket to specified host
socket_bind($socket, 0, $port);
//listen to port
socket_listen($socket);
$this->socket = $socket;
}
function __destruct()
{
foreach($this->clients as $client) {
socket_close($client);
}
socket_close($this->socket);
}
function run()
{
while(true) {
$this->waitForChange();
$this->checkNewClients();
$this->checkMessageRecieved();
$this->checkDisconnect();
}
}
function checkDisconnect()
{
foreach ($this->changed as $changed_socket) {
$buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
if ($buf !== false) { // check disconnected client
continue;
}
// remove client for $clients array
$found_socket = array_search($changed_socket, $this->clients);
socket_getpeername($changed_socket, $ip);
unset($this->clients[$found_socket]);
$response = 'client ' . $ip . ' has disconnected';
$this->sendMessage($response);
}
}
function checkMessageRecieved()
{
foreach ($this->changed as $key => $socket) {
$buffer = null;
while(socket_recv($socket, $buffer, 1024, 0) >= 1) {
$this->sendMessage(trim($buffer) . PHP_EOL);
unset($this->changed[$key]);
break;
}
}
}
function waitForChange()
{
//reset changed
$this->changed = array_merge([$this->socket], $this->clients);
//variable call time pass by reference req of socket_select
$null = null;
//this next part is blocking so that we dont run away with cpu
socket_select($this->changed, $null, $null, null);
}
function checkNewClients()
{
if (!in_array($this->socket, $this->changed)) {
return; //no new clients
}
$socket_new = socket_accept($this->socket); //accept new socket
$first_line = socket_read($socket_new, 1024);
$this->sendMessage('a new client has connected' . PHP_EOL);
$this->sendMessage('the new client says ' . trim($first_line) . PHP_EOL);
$this->clients[] = $socket_new;
unset($this->changed[0]);
}
function sendMessage($msg)
{
foreach($this->clients as $client)
{
@socket_write($client,$msg,strlen($msg));
}
return true;
}
}
(new MySocketServer())->run();
?>
You can easily extend the first example to handle any number of connections instead of jsut one
#!/usr/bin/env php
<?php
error_reporting(E_ALL);
/* Permitir al script esperar para conexiones. */
set_time_limit(0);
/* Activar el volcado de salida implícito, así veremos lo que estamo obteniendo
* mientras llega. */
ob_implicit_flush();
$address = '127.0.0.1';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}
//clients array
$clients = array();
do {
$read = array();
$read[] = $sock;
$read = array_merge($read,$clients);
// 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?");
continue;
}
// Handle new Connections
if (in_array($sock, $read)) {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
$clients[] = $msgsock;
$key = array_keys($clients, $msgsock);
/* Enviar instrucciones. */
$msg = "\nBienvenido al Servidor De Prueba de PHP. \n" .
"Usted es el cliente numero: {$key[0]}\n" .
"Para salir, escriba 'quit'. Para cerrar el servidor escriba 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
}
// Handle Input
foreach ($clients as $key => $client) { // for each client
if (in_array($client, $read)) {
if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($client)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
unset($clients[$key]);
socket_close($client);
break;
}
if ($buf == 'shutdown') {
socket_close($client);
break 2;
}
$talkback = "Cliente {$key}: Usted dijo '$buf'.\n";
socket_write($client, $talkback, strlen($talkback));
echo "$buf\n";
}
}
} while (true);
socket_close($sock);
?>