Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

fileevent on Win 32

by pg (Canon)
on Nov 06, 2004 at 20:38 UTC ( [id://405809]=perlquestion: print w/replies, xml ) Need Help??

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:

  1. Did I do anything wrong?
  2. Maybe someone can help test WIndows NT or Windows 95.
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

    Code tested as is on Win98/ActiveState 5.8.2 (identical results from MS_DOS Prompt and MSYS commandline):

    I then retested as before but with 'fileevent' commented out:

    With this test I noticed that the number displayed above the button incremented with each button click.

    In the previous tests, I waited until numbers() thread quiting ... was displayed before clicking the button. In this test, I started clicking the button as soon as the GUI appeared.



Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://405809]
Approved by atcroft
Front-paged by bart
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-06-19 20:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.