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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|