pg has asked for the wisdom of the Perl Monks concerning the following question:
To me, filevent is the best way to communicate between tk and your other background activities. Although fileevent does not work properly on Windows for most type of IO handlers, the author did claim that it worked with socket. I really wanted to see it work. Following is a piece of code I was testing, but it didn't work - callback was not triggered. (The claim was made for NT and 95 awhile back, but I am using XP.)
In order to test whether the socket part itself works, I added a button, which you can click and receive from the socket. (To make the button work, you have to comment out that fileevent line.)
My questions are:
use Tk; use threads; use IO::Socket::INET; use strict; $| ++; my $ss = IO::Socket::INET->new(Proto=>"tcp", LocalAddr=>"localhost", LocalPort=>3000, Listen=>10, Timeout=>60, Reuse=>1) || die "Failed to listen"; my $mw = MainWindow->new(); my $label = $mw->Label(-text => "0")->pack(); my $b = $mw->Button(-text => "Read ...", -command => \&callback)->pack +(); threads->create(\&numbers); print "Start to listen ...\n"; my $s = $ss->accept(); print "Connection established\n"; $b->fileevent($s, "readable" => \&callback); MainLoop; sub numbers { sleep(5); print "Entered numbers()\n"; my $c = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"localhost", PeerPort=>3000) || die "Failed to conne +ct"; for my $i (1..10) { print "about to send $i\n"; print $c "$i\n"; sleep(1); } print "numbers() thread quiting ...\n"; } sub callback { print "callback triggered\n"; my $l = <$s>; chomp $l; print "After read\n"; print $l; $label->configure(-text => $l); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: fileevent on Win 32
by Prior Nacre V (Hermit) on Nov 07, 2004 at 09:28 UTC |