allowing for multiple telnet connection on a socket server
Posted: Wed Dec 23, 2009 2:28 am
I am trying to run a script on server which would allow for remote telnet access using socket programming.It works fine as long as there is just one connection but as soon as another simultaneous telnet connection is opened data exchange becomes impossible.
Here is the code:-
<?
// don't timeout
set_time_limit (0);
// set some variables
//$host ="172.31.210.210";
//$port ="2100";
// create socket
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");
//socket_set_unblock($socket);/*i tried the unblock command here but to no avail*/[/b]
// bind socket to port
$result = socket_bind($socket,'172.31.210.210',2100) or die("Could not bind to socket\n");
//socket_set_nonblock($socket);
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "Waiting for connections...\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
//socket_set_block($spawn);
echo "Received connection request\n";
////////////////////
//$input = socket_read($spawn, 1024) or die("Could not read input\n");
// write a welcome message to the client
$welcome = "write some data!\n? ";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
// keep looping and looking for client input
do
{
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if (trim($input) != "")
{
// echo "Received input: $input\n";
// if client requests session end
if (trim($input) == "END")
{
// close the child socket
// break out of loop
socket_close($spawn);
break;
}
// otherwise...
else
{
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n");
// echo "Sent output: " . trim($output) . "\n";
// to write into file
$f = fopen("db.txt", "a");
fwrite($f, "$input\n");
fclose($f);
}
}
} while (true);
socket_close($spawn);
// close primary socket
socket_close($socket);
echo "Socket terminated\n";
?>
any clues ?
Bhaskar Goel
Here is the code:-
<?
// don't timeout
set_time_limit (0);
// set some variables
//$host ="172.31.210.210";
//$port ="2100";
// create socket
$socket = socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");
//socket_set_unblock($socket);/*i tried the unblock command here but to no avail*/[/b]
// bind socket to port
$result = socket_bind($socket,'172.31.210.210',2100) or die("Could not bind to socket\n");
//socket_set_nonblock($socket);
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "Waiting for connections...\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
//socket_set_block($spawn);
echo "Received connection request\n";
////////////////////
//$input = socket_read($spawn, 1024) or die("Could not read input\n");
// write a welcome message to the client
$welcome = "write some data!\n? ";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
// keep looping and looking for client input
do
{
// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
if (trim($input) != "")
{
// echo "Received input: $input\n";
// if client requests session end
if (trim($input) == "END")
{
// close the child socket
// break out of loop
socket_close($spawn);
break;
}
// otherwise...
else
{
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n");
// echo "Sent output: " . trim($output) . "\n";
// to write into file
$f = fopen("db.txt", "a");
fwrite($f, "$input\n");
fclose($f);
}
}
} while (true);
socket_close($spawn);
// close primary socket
socket_close($socket);
echo "Socket terminated\n";
?>
any clues ?
Bhaskar Goel