Where's the problem here?
| From: | René Fournier | Date: | Tue, 27 Apr 2004 16:38:10 +0000 |
| Subject: | Where's the problem here? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-184781@lists.php.net to get a copy of this message | ||
Hello,
I've got a multi-connection socket server working... sort of. It runs without errors, enters the loop, then waits for a connection. THEN, when I connect to it with a Telnet client, it starts looping without reading any input. And I can't figure out why. (The fact that I've just learned about sockets a week ago might have something to do with it.) Here's what happens:
1. Start the server.
[Valhalla:~/Sites/gpspolice/titan] rene% /usr/local/php/bin/php s15.php
Server port: 32000
Enter loop...
Set up clients...
2. In a different Telnet window, connect to the server.
[Valhalla:~] rene% telnet 192.168.0.200 32000
Trying 192.168.0.200...
Connected to 192.168.0.200.
Escape character is '^]'.
3. As soon as I connect, the server starts looping like crazy. Here's the server output window:
Set up clients...
Adding connection...
Set up clients...
Adding connection...
Set up clients...
Adding connection...
Set up clients...
Adding connection...
(forever)
4. While the server window goes nuts, I can type in the client side, but nothing happens, no return text, etc. Incidentally, although this code should accept more than one connection, when I try to connect to the server from another client, nothing happens—the connection is neither accepted nor rejected.
Here's my code (sorry for posting so much of it, but it's kinda long):
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
$max_clients = 10;
$null = null;
error_reporting(E_ALL);
ob_implicit_flush();
// Array will hold client info
$clients = Array();
// Create TCP stream socket
$sock = socket_create(AF_INET,SOCK_STREAM,0);
// Bind the socket
socket_bind($sock,$server_ip,$server_port) or die('Could not bind to address');
// Start listening...
socket_listen($sock);
echo "Enter loop...\n";
// Loop
while (true)
{
echo "Set up clients...\n";
// Setup clients listen socket for reading
$read[0] = $sock;
$cur_conn = count($clients);
for ($i = 0; $i < $cur_conn; $i++)
{
if ($clients[$i]['sock'] != $null)
{
$read[$i+1] = $clients[$i]['sock'];
echo "Set up client ".($i+1)."\n";
}
}
// Set up a blocking call to socket_select()
$ready = socket_select($read,$null,$null,$null);
echo "Adding connection...\n";
// If a new connection is being made add it to the client array
if (in_array($sock,$read))
{
for ($i = 0; $i < $cur_conn; $i++)
{
if ($clients[$i]['sock'] == $null)
{
$clients[$i]['sock'] = socket_accept($sock);
socket_write($clients[$i]['sock'],"Welcome to Jimmy!\n");
break;
} elseif ($i == $max_clients - 1)
print ("too many clients");
}
if (--$ready <= 0)
continue;
} // end if in_array
echo "Client writing...\n";
// If a client is trying to write, deal with it
for ($i = 0; $i < $cur_conn; $i++) // for each client
{
if (in_array($clients[$i]['sock'],$read))
{
$input = socket_read($clients[$i]['sock'],1024);
if ($input == $null) { // Zero length string meaning disconnected
echo "Input: null\n";
unset($clients[$i]);
}
if ($input == 'exit') { // requested disconnect
socket_close($clients[$i]['sock']);
echo "Input: 'exit'\n";
} elseif ($input) { // strip whitespace and writeback to user
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
socket_write($clients[$i]['sock'],$output);
echo "Sent output: " . trim($output) . "\n";
}
} else {
// Close the socket
socket_close($clients[$i]['sock']);
unset($clients[$i]);
}
}
} // end while
// Close the master sockets
socket_close($sock);