gokuraku has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

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
    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.

    As you currently have it, the server will wait for a new client connection after having closed the old connection. This is due to the loop

    while ( my $client = $server->accept( ) ) { ... }

    If you want it to return from sub server_run_single after the first connection has been closed, just don't use a loop, but rather simply

    my $client = $server->accept();
      Ah...I was hoping it could exit the while loop so I could start multiple servers over time. My expectation was the server may need to wait for clients, hence my use of the while here, but this was not working with close($client) since it just stayed in the loop.

      Gave this a try and my working code now looks like:

      sub server_run_single() { my ($server,$keepAlive,$i,$loop,$verbose) = @_; my $d_count = 0; my $result = 0; print "Running Server loop $i of $loop.\n" if $verbose; # accept and process connections 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); # 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; }

      Thanks!

Re: Getting Client to Disconnect
by ikegami (Patriarch) on Dec 17, 2007 at 21:46 UTC

    I'm not sure if there is something other than close($client) that would do what I want

    Do you have a reason to want something other than close($client)?