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); }

In reply to fileevent on Win 32 by pg

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.