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


In reply to Getting Client to Disconnect by gokuraku

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.