I am having trouble operating this code.
<?php
ob_end_flush();
ob_implicit_flush(true);
set_time_limit(0);
function consoleEcho($msg, $DontShowDate = 0)
{
if(!$DontShowDate)
$date = "[". date('m/d H:i:s'). "] ";
$msg = $date . $msg;
print($msg ."\n");
}
class client
{
public $sock;
public $ipaddr;
public $chatServer;
public $uid;
function sendMessage($msg)
{
socket_write($client[$i]->sock, $msg . chr(0));
}
function serverMessage($msg)
{
socket_write($client[$i]->sock, $msg . chr(0));
}
function privateMessage($from, $msg)
{
socket_write($client[$i]->sock, "PM\t". $from ."\t". $msg . chr(0));
}
function disconnect()
{
consoleEcho(get_class($i));
socket_close($this->sock);
$i = key($this);
$this->chatServer->messageAll("Client ". $this->uid ." disconnected");
consoleEcho("Client ". $this->uid ." disconnected");
unset($this);
}
}
class chat
{
public $sock;
public $client = array();
function messageAll($msg)
{
$client =& $this->client;
for($i = 0; $i < count($client); $i++)
@socket_write($client[$i]->sock, $msg . chr(0));
}
function getTotalClients()
{
}
function start()
{
$host = "192.168.1.2";
$port = 9999;
$max = 500;
$client =& $this->client;
$sock =& socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("[". date('Y-m-d H:i:s') ."] Could not create socket\n");
socket_bind($sock, $host, $port) or die("[". date('Y-m-d H:i:s'). "] Could not bind to socket\n");
socket_listen($sock) or die("[". date('Y-m-d H:i:s') ."] Could not set up socket listener\n");
consoleEcho("Server started on ". $host .":". $port ."\n\n");
$beepExec = microtime();
$f = fopen("beepTime.txt", "w");
fwrite($f, $beepExec);
fclose($f);
while(true)
{
if($beepExec != file_get_contents("beepTime.txt"))
{
$beepExec = microtime();
$f = fopen("beepTime.txt", "w");
fwrite($f, $beepExec);
fclose($f);
$file = explode(" ", file_get_contents("beep.txt"));
$this->messageAll("[beep.exe ". $file[0] ." ". $file[1] ."]");
echo("Beep sent\n");
}
usleep(10000);
socket_set_block($sock);
$read[0] = $sock;
for($i = 0; $i < $max; $i++)
if($client[$i]->sock != null)
$read[$i+1] = $client[$i]->sock;
$ready = @socket_select($read, $write = NULL, $except = NULL, 0.001);
if(in_array($sock, $read))
{
for($i = 0;$i<$max;$i++)
{
if($client[$i] == null)
{
$client[$i] = new client();
$client[$i]->chatServer = $this;
$client[$i]->uid = $i;
}
if($client[$i]->sock == null)
{
if(($client[$i]->sock = socket_accept($sock)) < 0)
{
consoleEcho("socket_accept() failed: ". socket_strerror($client[$i]->sock));
}
else
{
socket_getpeername($client[$i]->sock, $client[$i]->ipaddr);
if($i == $max - 1)
{
consoleEcho("Too many clients");
$client[$i]->disconnect();
}
consoleEcho("Connection #".$i." from ". $client[$i]->ipaddr);
//$this->messageAll("Client #".$i." connected");
//socket_write($client[$i]->sock, "[beep.exe 1000 1000]" . chr(0));
socket_write($client[$i]->sock, $client[$i]->ipaddr . chr(0));
}
break;
}
}
if(--$ready <= 0)
continue;
}
for($i=0;$i<$max;$i++)
{
if(in_array($client[$i]->sock, $read))
{
$input = @socket_read($client[$i]->sock, 1024);
//if($input == null)
//$client[$i]->disconnect();
$n = trim($input);
$com = split(" ",$n);
if($n == "EXIT")
{
if($client[$i]->sock != null)
{
// Disconnect requested
$client[$i]->disconnect();
//socket_close($client[$i]->sock);
//unset($client[$i]->sock);
//consoleEcho("Disconnected(2) client #".$i);
//$this->messageAll("Client ". $i ." disconnected");
if($i == $adm)
$adm = -1;
}
}
else if($n == "TERM")
{
// Server termination requested
socket_close($sock);
consoleEcho("Terminated server (requested by client #".$i.")");
exit();
}
else if($input)
{
if($n=="PING")
{
socket_write($client[$i]->sock, "PONG". chr(0));
}
}
}
else
{
/*if($client[$i]->sock == null)
{
// Close the socket
socket_close($client[$i]->sock);
unset($client[$i]);
consoleEcho("Disconnected(1) client #".$i);
}*/
}
}
}
socket_close($sock);
}
}
$chat = new chat();
$chat->start();
?>
I can connect to my server on 192.168.1.2 easily from my computer on 192.168.1.3 (connecting through kalphiter.com:9999). The only issue is that the socket server says the connecting IP address is 192.168.1.1.
That's not a major issue, but I have an even bigger issue because the connection is reset immediately when I connect from school. I have opened the firewall on the server using "ufw allow 9999". What's odd is that the server reports that it received a connection.
I am using Ubuntu 9.10.