我正在开发iPhone和PHP的视频聊天应用 我通过我的iPhone客户端的套接字将图像发送到连接到该端口的其他iPhone客户端
问题:我们通过iPhone客户端每隔3秒循环传递图像 图像成功通过,但5分钟后无法发送图像 请让我知道可能是什么问题 请检查我的代码并告诉我在代码中添加/编辑的内容和位置
我的代码在下面
1)SocketServer.class.php
<?php
ob_implicit_flush();
set_time_limit(0);
class SocketServer
{
protected $config;
protected $hooks;
protected $master_socket;
public $max_clients = 10;
public $max_read = 2048; //New Change
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, SOL_TCP);
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}
socket_bind($this->master_socket,$this->config["ip"],$this->config["port"]);
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 = 50) < 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="";
$input = socket_read($this->clients[$i]->socket, $this->max_read,PHP_BINARY_READ);
if($input == null)
{
$this->disconnect($i);
}
else
{
for($a=0; $a < count($this->clients); $a++ ){
if($a != $i ){
$this->trigger_hooks("INPUT",$this->clients[$a],$input);
}
}
$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}\r\n");
}
public static function socket_write_smart(&$sock,$string,$crlf = "\r\n")
{
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("Client ".$i." 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});
}
}
?>
2)callsocket.php
<?php
ob_implicit_flush();
ignore_user_abort(true);
function socketError($errorFunction, $die=false) {
$errMsg = socket_strerror(socket_last_error());
}
require_once("SocketServer.class.php"); // Include the Class File
$server = new SocketServer('127.0.0.1', 5585 );
$server->max_clients = 10; // Allow no more than 10 people to connect at a time
$server->hook("CONNECT","handle_connect"); // Run handle_connect everytime 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.
///code to destroy when terminal closes
$server->destroy();
function handle_connect(&$server,&$client,$input) {
//SocketServer::socket_write_smart($client->socket,"String?sss ","");
}
function handle_input(&$server,&$client,$input) {
$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...");
$server->disconnect($client->server_clients_index); // Disconnect this client.
return; // Ends the function
}
SocketServer::socket_write_smart($client->socket,$input,""); // Send the Client back the String
}
?>
测试文件 通过telnet命令提示符执行callsocket.php,如= php callsocket.php 之后,我们可以使客户端像telnet 127.0.0.1 5585
它还通过iPhone客户端发送图像 但5-7分钟后未能发送 请看我的代码并帮助我 任何形式的帮助都将受到高度赞赏