Hello Monks,

I want a Tkx application that shows the messages coming in via TCP ("real time", single-line messages). The Tkx part does not have any controls for the user (except "exit"), and there is only one client sending messages. I thought this would be easy but not so. The Cookbook does not have a working example, and my Tk book does not even start the subject. After a hint I made this (not working)
It seems to me that the fileevent in Tk is very different from Tkx

#!/opt/ActivePerl-5.12/bin/perl use strict; use warnings; use Tkx; Tkx::package_require('tile'); my $mw = Tkx::widget->new("."); use IO::Socket; my $server = IO::Socket::INET->new( LocalPort => 7777, Type => SOCK_STREAM, Reuse => 1, Listen => 3 ); my $log = $mw->new_tk__text( -height => 10, -width => 60, -wrap => 'none' ); $log->g_grid( -column => 0, -row => 0 ); Tkx::fconfigure($server, -blocking => 0); Tkx::fileevent( $server, readable => [\&new_connection, \$server] ); Tkx::MainLoop(); sub new_connection { my $listen = shift; my $client = $listen->accept() or warn "Can't accept connection"; Tkx::fconfigure($client, -blocking => 0); Tkx::fileevent( $client, readable =>[\&handle_connection, \$client +] ); $log->insert( 'end', "connected\n" ); $log->see('end'); Tkx::update(); } sub handle_connection { my ($client) = shift; my $message = <$client>; if ( defined $message and $message !~ /^quit/ ) { $message =~ s/[\r\n]+$//; $log->insert( 'end', "$message\n" ); $log->see('end'); Tkx::update(); } else { print "connection closed\n"; $log->insert( 'end', "connection closed\n" ); $log->see('end'); Tkx::update(); $client->close(); } }
It generates an error: can not find channel named "IO::Socket::INET=GLOB(0xa119ed0)" at ./sps +how_simple.pl line 24. A client (not tested either because of failing server...)
#!/usr/bin/perl use IO::Socket; my $machine_addr = 'localhost'; $sock = new IO::Socket::INET(PeerAddr=>$machine_addr, PeerPort=>7777, Proto=>'tcp', ); die "Could not connect: $!" unless $sock; foreach my $count(1..100){ print $sock "$count\n"; print "$count\n"; } close ($sock);
Thank you

In reply to Tkx and tcp server by momo33

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.