Well the code was from a complete example set which I attached to my email response to your question. I include it below in the readmore, in case you lost the attachments, or some other mailer-incompatibility problems. It is a basic server, and a Tk client. The client sends what is typed in the bottom entry box, and displays the return in the top text box. It is basically what you want to do. You can see that it is basically very simple, when chopped down to just network and Tk code. The fileevent just takes the socket itself, and watches it for when it is readable. When it is, it prints it out. But fileevent will NOT block the rest of the script, like a while loop will. fileevent is basically Tk's version of IO::Select, but adapted to work with the event loop.
#### the basic server ##################### #!/usr/bin/perl -w use strict; use IO::Select; use IO::Socket; $| = 1; # create the socket #my $host = shift || 'localhost'; #my $host = '192.168.0.1'; my $host = 'localhost'; my $port = 12345; my $socket = IO::Socket::INET->new( LocalPort => $port, LocalAddr => $host, Listen => 5, Proto => 'tcp', Reuse => 1, ); defined $socket or die "ERROR: Can't create socket: $!\n"; print STDERR "Socket open ... listening for incoming calls ..\n"; my $select = IO::Select->new($socket); my %socks; while (1) { foreach my $fh ($select->can_read) { if ($fh == $socket) { # new connection my $new = $socket->accept; $select->add($new); $socks{$new}{FH} = $new; print STDERR "Received new connection ($new) ..\n"; } else { my $data = <$fh>; if (defined $data) { $data =~ tr/\r\n//d; foreach my $handle (keys %socks) { print {$socks{$handle}{FH}} "$handle $data\n"; } } else { print "BYE $fh.\n"; $select->remove($fh); delete $socks{$fh}; $fh->close; } } } } __END__ ######### the Tk client #################### #!/usr/bin/perl use warnings; use strict; use Tk; use IO::Socket; require Tk::ROText; # create the socket my $host = shift || 'localhost'; my $port = 12345; my $socket = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', ); defined $socket or die "ERROR: Can't connect to port $port on $host: $ +!\n"; print STDERR "Connected to server ...\n"; my $mw = new MainWindow; my $log = $mw->Scrolled(qw/ROText -scrollbars ose/)->pack; my $txt = $mw->Entry()->pack(qw/-fill x -pady 5/); $mw ->bind('<Any-Enter>' => sub { $txt->Tk::focus }); $txt->bind('<Return>' => [\&broadcast, $socket]); $mw ->fileevent($socket, readable => sub { my $line = <$socket>; unless (defined $line) { $mw->fileevent($socket => readable => ''); return; } $log->insert(end => $line); }); MainLoop; sub broadcast { my ($ent, $sock) = @_; my $text = $ent->get; $ent->delete(qw/0 end/); print $sock $text, "\n"; } __END__

I'm not really a human, but I play one on earth. flash japh

In reply to Re^3: perl/tk sockets and fileevent by zentara
in thread perl/tk sockets and fileevent by HeatSeekerCannibal

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.