The 'accept' method won't turn the process loose to allow me to rebuild parts of the GUI diplay after receiving data from the client. It just sits and continues to listen. I understand that the MainLoop is looking for an event to handle but fileevent goes into an endless loop when I try to use that and anything else just locks up after the first client connection.

Without seeing your code, it sounds like the socket is blocking. Tk has 2 ways of dealing with it, Tk::Fileevent and Tk::After. You have to let Tk's methods handle the socket reading, so it won't block the "event loop".

Here is an example that was posted awhile back:

#!/usr/bin/perl use warnings; use strict; use Tk; use Symbol qw(gensym); use IO::Socket; $ENV{HOME} = "/" unless $ENV{HOME}; print( "Listening on localhost port 31415. Telnet to it and try\n", "entering something. In an ideal world, it should echo back.\n +" ); my $acceptor = IO::Socket::INET->new ( LocalAddr => '127.0.0.1', LocalPort => 31415, Listen => 5, Reuse => 'yes', ); my $client = $acceptor->accept(); die "Could not accept a client: $!" unless defined $client; print "Client connection accepted.\n"; # Turn off buffering. select((select($client), $| = 1)[0]); # Send a welcome message. print $client "Hello!\x0d\x0a"; print "Setting callback for $client.\n"; my $main_window = Tk::MainWindow->new(); $main_window->fileevent( $client, 'readable', [ \&read_client ] ); print "Callback set.\n"; Tk::MainLoop(); exit 0; sub read_client { print "Client callback has been invoked.\n"; my $line = <$client>; exit unless defined $line; print $client $line; }

I'm not really a human, but I play one on earth. flash japh

In reply to Re: TK MainLoop and Sockets by zentara
in thread TK MainLoop and Sockets by Anonymous Monk

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.