Hi everybody!

I cannot explain why server doesn't close TCP session.

Code:
#!/usr/bin/perl -w use threads ('yield', 'stack_size' => 64*4096, 'exit' => 'threads_only', 'stringify'); use IO::Socket::INET; my %shared_value :shared; sub sleeping { { lock %shared_value; #do somesing with %shared_value #... } sleep 60; } my $commandLinePrefix = "Command: "; sub listeningClient { my $client = shift; my $clientHost = $client->peerhost; $client->autoflush(1); print $client "Welcome to $0; type help for command list.\n$comman +dLinePrefix"; while ( <$client> ) { next unless /\S/; if ( /quit|exit|^q\n?\r?$/i ) { print $client "Closing connection\n"; last; } elsif ( /^sleep|^s\n?\r?$/i ) { threads->create( \&sleeping )->detach(); } else { print $client <<EOF; COMMANDS: quit|exit|q - quit from current session sleep|s - fall asleep EOF } } continue { print $client $commandLinePrefix; } close $client; } sub startServer { $IP = shift || 'localhost'; $PORT = shift || 9000; $server = IO::Socket::INET->new( Proto => "tcp", LocalAddr => $IP.':'.$PORT, Listen => SOMAXCONN, Reuse => 1); unless( $server ) { print "can't setup server. Error: $@\n"; exit; } print "[Server $0 accepting clients]\n"; while( my $connection = $server->accept() ) { my $thr = threads->create( \&listeningClient, $connection ); close $connection; } } sub main { my $ip = 'localhost'; my $port = 9000; use Getopt::Long; GetOptions( "ip=s" => \$ip, "port=i" => \$port ); startServer( $ip, $port ); } main();

Testing the server:

$ telnet 0 9000 Trying 0.0.0.0... Connected to 0. Escape character is '^]'. Welcome to ./strange.pl; type help for command list. Command: sleep Command: quit Closing connection

I expect than connection is closed immediately but connection is closed in a minute.

How to call "sleeping" function in a new thread and allow to close the session immediately (quit command)?


In reply to unclear TCP client session block by Evgenij

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.