Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi, I have made the following modifications in the server code for asynchronous communication, but the server gui hangs as I start the client.

use strict; use warnings; use Tk; use IO::Socket; use IO::Select; my $status = ''; my $mw = MainWindow->new; $mw->geometry("200x200"); my $buttonFrame = $mw->Frame()->pack(-side => 'bottom'); $buttonFrame->Button(-text => 'Start', -command => sub { init_server(\$mw, \$status) } )->pack(-side => 'left'); my $displayFrame = $mw->Frame()->pack(-side => 'top'); $displayFrame->Label(-text => 'Status:')->pack(-side => 'left'); $displayFrame->Label(-textvariable => \$status)->pack(-side => 'left') +; my $server; MainLoop; sub init_server { my ($mw_ref, $status_ref) = @_; $server = IO::Socket::INET::->new( Proto => 'tcp', LocalPort => 55555, Listen => 1, Reuse => 1 ) or die "Server can't start: $!"; =cut my $client = $server->accept(); $client->autoflush; $$mw_ref->fileevent($client, 'readable', sub { if (defined(my $read = <$client>)) { chomp $read; $$status_ref = $read; } }); =cut #my $client = $server->accept(); $$mw_ref->fileevent($server, 'readable', sub { my $readable_handles = new IO::Select(); $readable_handles->add($server); my $buf; while (1) { #Infinite loop # select() blocks until a socket is ready to be read or written my ($new_readable) = IO::Select->select($readable_handles, undef, undef, 0); print "Inside while \n"; # If it comes here, there is at least one handle # to read from or write to. For the moment, worry only about # the read side. foreach my $sock (@$new_readable) { print "Inside foreach $sock \n"; if ($sock == $server) { my $new_sock = $sock->accept(); # Add it to the list, and go back to select because the # new socket may not be readable yet. $readable_handles->add($new_sock); } else { # It is an ordinary client socket, ready for reading. $buf = <$sock>; if ($buf) { # .... Do stuff with $buf $$status_ref = $buf; } else { # Client closed socket. We do the same here, and remov +e # it from the readable_handles list $readable_handles->remove($sock); close($sock); } } } } } ); }

In reply to Re^2: asynchronous socket communication with perl tk by simonz
in thread asynchronous socket communication with perl tk by simonz

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (11)
    As of 2015-07-02 22:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (46 votes), past polls