On Tue, 22 Jan 2002 14:47:00 +0000 (GMT), Srinivas krish wrote: > Hi, > > I want to start a server and a client script using a > perl scripting. I was wondering if this can be > efficiently done using the multithreading feature in > perl script. Can anyone give sample scripts that > brings up some server and a client using the same > script. > > Also is there anyway to bring down the server after > the client processes are over? Also how do we ensure > that the client starts after the server has > started.(do we need to sleep the server process for a > while)
Here is a sample program using a multitasking and networking framework called POE. The included program implements a single socket server and a small set of simultaneous clients. POE uses cooperative multitasking. Code like { sleep(60) } must be written differently or it will block the entire program. Most of these blocking functions can be written in other ways, and POE provides the basic libraries to do so. -- Rocco Caputo / [EMAIL PROTECTED] / poe.perl.org / poe.sf.net #!/usr/bin/perl # This sample program requires POE, which can be found on the CPAN or # at http://poe.perl.org/ use warnings; use strict; sub MAXIMUM_SIMULTANEOUS_CLIENTS () { 5 } my $clients_left = MAXIMUM_SIMULTANEOUS_CLIENTS; use POE; use POE::Component::Server::TCP; use POE::Component::Client::TCP; #-------------------------------------------------------------------- # This is a simple echo server, listening on port 32000 on the # localhost interface. It supports several simultaneous connections. POE::Component::Server::TCP->new ( Alias => "echo_server", Address => "127.0.0.1", Port => 32000, ClientConnected => sub { print "Server notes a client connection.\n"; }, ClientDisconnected => sub { print "Server notes a client disconnect.\n"; }, # KERNEL is a reference to POE's main multitasking framework. It # provides several services, including asynchronous I/O, events, # and timers. # HEAP is a storage space unique to this client/server session. # It can be thought of as per-thread storage. The "client" member # represents this server's connection to a client. ClientInput => sub { my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0]; print "Server got input: $input\n"; $heap->{client}->put($input); $kernel->yield("shutdown") if $input eq 'goodbye'; } ); #-------------------------------------------------------------------- # This is a simple echo client. It will connect to localhost 32000, # send a couple things, and disconnect. foreach my $client_number (1..MAXIMUM_SIMULTANEOUS_CLIENTS) { POE::Component::Client::TCP->new ( Alias => "echo_client", RemoteAddress => "127.0.0.1", RemotePort => 32000, Connected => sub { my $heap = $_[HEAP]; $heap->{count} = 1; # Count the number of messages sent. print "Client $client_number connected to server.\n"; $heap->{server}->put("Client $client_number says: $heap->{count}"); }, # The client has disconnected. The last client will also shut # down the server. Disconnected => sub { print "Client $client_number has disconnected.\n"; $_[KERNEL]->post( echo_server => "shutdown" ) unless --$clients_left; }, # Handle input from the server. ServerInput => sub { my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0]; print "Client $client_number received input from server: $input\n"; if ($heap->{count} >= 5) { $heap->{server}->put("goodbye"); return; } $heap->{count}++; $heap->{server}->put("Client $client_number says: $heap->{count}"); }, ); } $poe_kernel->run(); exit 0; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]