gokuraku has asked for the wisdom of the Perl Monks concerning the following question:
I'm still new to client and server connections in Perl, and have been working with TCP Clients and Servers from the Cookbook and some other sources.
I've got some code for a Server that should be disconnecting on a specific input string, the problem I am having is getting the client to disconnect so the Server can run on multiple loops.
Here is the code I have, this function is supposed to run multiple loops of the $server object in the server_run_single function
sub server_run_type { my ($server,$keepAlive,$loop,$verbose) = @_; my $i; my $result; if ($sys =~ m/win/ || $loop) { print "Running single model, " if $verbose; print "with Keep Alive = $keepAlive.\n" if $verbose; # Single model for (my $i = 1; $i < $loop; $i++) { $result = &server_run_single($server,$keepAlive,$i,$loop,$v +erbose); } } else { print "Running multi model with $clientCount client(s), " if $v +erbose; print "with Keep Alive = $keepAlive.\n" if $verbose; # Forked model $result = &server_run_multi($server,$keepAlive,$verbose); } close($server); return $result; }
The single model when it gets the "disconnect" string should shut down the client and restart in a loop within the server_run_type function.
sub server_run_single() { my ($server,$keepAlive,$i,$loop,$verbose) = @_; my $d_count = 0; my $result = 1; print "Running Server loop $i of $loop.\n" if $verbose; # accept and process connections while ( my $client = $server->accept( ) ) { $clientCount = &add_Clientcount($verbose); print "Connected - " . &id_client($client) . "\n"; while ( defined (my $data_recv = <$client>) ) { if ($data_recv =~ m/discon/) { $clientCount = &subt_Clientcount($verbose); $d_count += 1; if ($d_count == $loop) { print "Disconnects = $d_count.\n" if $verbose; # Get out of this loop close($client); } } print STDOUT $data_recv if $verbose; print $client "You said - $data_recv\n" if $verbose; } } print "Exiting loop $i of $loop.\n" if $verbose; return $result; }
I'm not sure if there is something other than close($client) that would do what I want, I've tried a couple of different methods but they do not seem to work. I'm sure there is a better way to handle this and get the script to work what I want, I just can't seem to find it.
Any help is appreciated.
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting Client to Disconnect
by almut (Canon) on Dec 17, 2007 at 22:36 UTC | |
by gokuraku (Monk) on Dec 18, 2007 at 12:30 UTC | |
|
Re: Getting Client to Disconnect
by ikegami (Patriarch) on Dec 17, 2007 at 21:46 UTC |