in reply to TK MainLoop and Sockets

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

Replies are listed 'Best First'.
Re: Re: TK MainLoop and Sockets
by Anonymous Monk on May 04, 2004 at 02:22 UTC
    zentara, I really appreciate your help. This script you have provided is the same as I am using, for the most part. I have the same problem here. The 'fileevent' executes on the event, but only once. I need it to execute on the accept(), move to the callback and then set up for another accept(). I didn't use the telnet part. I send from another socket. That info gets there, once. As the info changes, I want to resend to this server and update the GUI. Or at least that's what I'm trying to accomplish. wildman