You can do this with a non-blocking socket and Tk's repeat method (see Tk::after). This is untested, but something like it should work:
sub checksocket { my ($sock) = @_; my $data; $sock->read($data, $nnn) # replace $nnn with number of bytes you +want or return; # no data read # do something with $data here } my $mw = MainWindow->new; # set up your Tk widgets here my $sock = IO::Socket::INET->new(PeerAddr => 'host:port', Proto => 'tcp', Blocking => 0); # set non-blocking mode my $after = $mw->repeat(500, # set to appropriate number of milis sub { checksocket($sock) } ); MainLoop;
This will cause Tk to do its usual thing, except that it will interrupt the event loop every however-many milliseconds to call your subroutine, which will attempt to read data from the socket in non-blocking mode.

In reply to Re: Listening Socket in Tk by Errto
in thread Listening Socket in Tk by avo

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.