服务器无法从PHP套接字读取数据
我正在创建一个命令行PHP程序,它使用套接字监听与telnet连接的用户,读取要在服务器数据库上运行的命令 问题是我无法获取发送回服务器的内容。这是系统代码服务器无法从PHP套接字读取数据,php,sockets,Php,Sockets,我正在创建一个命令行PHP程序,它使用套接字监听与telnet连接的用户,读取要在服务器数据库上运行的命令 问题是我无法获取发送回服务器的内容。这是系统代码 <?php /*****************************************************************/ /* */ /* Welcome to the ISH.
<?php
/*****************************************************************/
/* */
/* Welcome to the ISH. */
/* You will need to configure this file to connect to Gen's */
/* Servers correctly. */
/* It is possible to be banned from this tool. Do NOT abuse it. */
/* -Alex */
/*****************************************************************/
//Configuration
error_reporting(0);
$connection_ip = "*****";
$connection_port = 43597;
$max_clients = 10;
$Server = new GenISH();
$Server->databaseConnect();
$Server->connect();
$Server->bind();
$Server->listen();
$Server->MainLoop();
class GenISH
{
public $connection_ip;
public $connection_port;
public $socket;
public $max_clients;
public $read;
public $connected_clients;
public $databaseConnection;
//User data
public $username;
public function checkBanned($ip)
{
$query = mysqli_query($this->databaseConnection, "SELECT * FROM `ish_bans` WHERE `ip` = '$ip'");
if(mysqli_num_rows($query) >= 1)
{
return true;
}else
{
return false;
}
}
public function getBanReason($ip)
{
$query = mysqli_query($this->databaseConnection, "SELECT * FROM `ish_bans` WHERE `ip` = '$ip'") or die(mysqli_error($this->databaseConnection));
$row = mysqli_fetch_assoc($query);
return $row["reason"];
}
public function databaseConnect()
{
$this->databaseConnection = mysqli_connect("localhost", "root", "GenDatabase123", "gen");
echo "Server: Connected to database\n";
}
public function showError($error)
{
$ec = socket_last_error();
$errmsg = socket_strerror($ec);
echo $error = "Error! Could not $error socket! [$ec] $errmsg";
}
public function __construct()
{
global $connection_ip, $connection_port, $max_clients;
$this->connection_ip = $connection_ip;
$this->connection_port = $connection_port;
$this->max_clients = $max_clients;
}
public function connect()
{
echo "Server: Connecting!\n";
if(!($this->socket = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$this->showError("connect");
}else
{
echo "Server: Connected!\n";
}
}
public function bind()
{
echo "Server: binding socket!\n";
if(!(socket_bind($this->socket, $this->connection_ip, $this->connection_port)))
{
echo "Server: Failed to bind!\n";
}else
{
echo "Server: Socket bind successful!\n";
}
}
public function listen()
{
if(!(socket_listen($this->socket, 10)))
{
echo "Server: Failed to listen on socket!\n";
}else
{
echo "Server: Initialization complete!\nServer: Listening for connections on port $this->connection_port!\n";
}
}
public function send($client, $message)
{
socket_write($client, $message);
}
public function getUserData($client)
{
$query = mysqli_query($this->databaseConnection, "SELECT * FROM `users` WHERE `ip_last` = '$client'");
if(mysqli_num_rows($query) <= 0)
{
$this->username = "Unkown...";
}else
{
$row = mysqli_fetch_assoc($query);
$this->username = $row["username"];
}
}
public function MainLoop()
{
$this->read = array();
$this->connected_clients = array();
while(true)
{
$this->read = array();
//Set Master socket
$this->read[0] = $this->socket;
for($i = 0; $i < $this->max_clients; $i++)
{
if($this->connected_clients[$i] != null)
{
//An exmple of how this works is
//this adds one to the read and makes it equal to the connected client.
$this->read[$i+1] = $this->connected_clients[$i];
}
}
if(socket_select($this->read, $write, $except, null) == false)
{
$this->showError("listen on");
}
if(in_array($this->socket, $this->read))
{
for($i = 0; $i < $this->max_clients;$i++)
{
//Null clients mean their connection has not been accepted.
if($this->connected_clients[$i] == null)
{
$this->connected_clients[$i] = socket_accept($this->socket);
if(socket_getpeername($this->connected_clients[$i], $ip, $port))
{
if($this->checkBanned($ip) == true)
{
echo "Connection from $ip was blocked.\n";
socket_write($this->connected_clients[$i], "You are banned from using the ISH for the following reason:");
$reason = $this->getBanReason($ip);
socket_write($this->connected_clients[$i], "\r\n");
socket_write($this->connected_clients[$i], $reason);
socket_close($this->connected_clients[$i]);
}else
{
echo "Connection from $ip was accepted.\n";
$this->send($this->connected_clients[$i], "Connection accepted!\n");
$this->send($this->connected_clients[$i], "\r\n");
$this->send($this->connected_clients[$i], "Retrieving user data.\n");
$this->getUserData($ip);
$this->send($this->connected_clients[$i], "\r\n");
$this->send($this->connected_clients[$i], "Welcome ".$this->username);
}
}
break;
}
}
}
for($i = 0; $i < $this->max_clients; $i++)
{
if(in_array($this->connected_clients[$i], $this->read))
{
$input = socket_read($this->connected_clients[$i], 1024);
if($input == null)
{
if(socket_getpeername($this->connected_clients[$i], $ip, $port))
{
echo "$ip disconnected from port $port.\n";
}
socket_close($this->connected_clients[$i]);
unset($this->connected_clients[$i]);
}
}
}
if($input == "login")
{
echo "LOGIN CALLED!";
}else
{
echo $input;
}
}
}
}
?>
在Stackoverflow,我们想知道您的问题,而不是系统手册。事实上,您应该将整个过程简化为一个简单的测试用例(不包含所有数据库部分),例如20行或更少,并更多地关注您尝试读取输入的代码。否则,即使经过大量编辑,它也可能被其他人关闭。顺便说一句,由于您实际上是在创建代码,请记住mysql和mysqli扩展已被弃用,应该用PDO替换。@LelioFaieta mysqli(尚未)。。。但我也不喜欢;)你是Java程序员吗?你为什么要编写自己的telnet服务器?@symcbean这是为了在我的服务器上进行测试,看看我能在PHP上走多远。我只是觉得拥有它会很酷。
if($input == "login")
{
echo "LOGIN CALLED!";
}else
{
echo $input;
}