Got a bit of a dilema with a script I've been working on for some time now.... It works 100% other than the fact that it can only accept ONE stinking connection at a time. In fact, a second "attempt" by another client will crash the script.
Anyway, I'm relatively sure I'll need to fork the process, from what I've read, but after about 4 hours of failed attempts at doing so... HELP!
<?php
require_once("global.php");
error_reporting (E_ALL); // Error Reporting Maximum
set_time_limit (120); // Timeout after this amount of Seconds
ob_implicit_flush (); // Don't Buffer Output To Browsers
// Define Static Varibles And Configuration
$shutdown_command = "closeserversockets"; // Command to SHUTDOWN the Service
$db_host = "localhost"; // mySQL Host System
$db_user = "*****"; // mySQL Login Name
$db_passwd = "*****"; // mySQL Login Password
$db_name = "*****"; // mySQL Database Name
$pop3_user_table = "users";
$pop3_message_table = "boxes";
$max_sockets = 5;
$vers = "MBoxMail v0.0.1.a";
$welcome_message = "HELLO (".$vers.")";
$address = 'xxx.xxx.xxx.xxx'; // Server Address (Used By Socket)
if (!$port)
{
$port=1234;
}
// Just initialize the following variables....
$user = "";
$pass = "";
$logged_in= false;
// End Varibale Initialization
// End Variable and Configuration Definitions
mysql_connect("$db_host","$db_user","$db_passwd") or die("Fatal Error: Unable to connect to mySQL!");
mysql_select_db("$db_name") or die("Fatal Error: Unable to open \"$dbname\" database!");
// Create Socket Commands
do
{
if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0)
{
die ("socket_create() failed: reason: " . socket_strerror ($sock));
}
if (($ret = socket_bind ($sock, $address, $port)) < 0)
{
die ("socket_bind() failed: reason: " . socket_strerror ($ret));
}
if (($ret = socket_listen ($sock, $max_sockets)) < 0)
{
die ("socket_listen() failed: reason: " . socket_strerror ($ret));
}
if (($msgsock = socket_accept($sock)) < 0)
{
die ("socket_accept() failed: reason: " . socket_strerror ($msgsock));
}
else
{
}
if (!write($welcome_message))
{
die ("FATAL ERROR: Unable To Send Initialization Messave Via Socket!");
}
// MAIN COMMAND PARSER LOOP HERE
do
{
$waitfor = strtolower(waiting(90));
if ($waitfor)
{
echo "Got ".$waitfor."<br>\n";
flush();
if (strstr($waitfor,$shutdown_command))
{
socket_close ($msgsock);
socket_close ($sock);
exit("\nReceived authorized shutdown command. Sockets Closed.");
}
if (strstr($waitfor,"quit"))
{
socket_close ($msgsock);
socket_close ($sock);
break;
}
// ... various other commands & responses
}
} while(true);
} while(true);
exit;
?>
Not sure just how many sockets one can effectively manage with a single instance of the script invoked... but hopefully someone here a bit more familiar with sockets / forks will be able to shed some much needed light on my little project....
Thanks
Phantazm