allowing for multiple telnet connection on a socket server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bhaskar_goel
Forum Newbie
Posts: 2
Joined: Wed Dec 23, 2009 2:19 am

allowing for multiple telnet connection on a socket server

Post by bhaskar_goel »

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: allowing for multiple telnet connection on a socket server

Post by requinix »

No "data exchange" on the second connection? Well yeah. Your script is only set up to handle one connection at a time. Then when it's done with that client the server quits.
bhaskar_goel
Forum Newbie
Posts: 2
Joined: Wed Dec 23, 2009 2:19 am

Re: allowing for multiple telnet connection on a socket server

Post by bhaskar_goel »

ok i got that, how do i go about doing it for multiple connections then?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: allowing for multiple telnet connection on a socket server

Post by requinix »

Well, there are a few ways:

1. One client at a time. Hardly ever useful.
2. One client, one server thread. Good, but can swamp the machine with lots of threads.
3. Thread pool.

I'm not going to give a tutorial on the forking server pattern (#2) in PHP, but
1. Server starts up and listens for connections
2. Server waits for a connection
3. A client connects
4. Server creates a thread (or forks the current one) to handle it
5 (parent). Server looks for dead children and goes back to step #3
5 (child). Thread handles client and terminates

pcntl_fork() and pcntl_wait() can help.
Post Reply