momo33 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Tkx and tcp server
by zentara (Cardinal) on Mar 18, 2011 at 15:05 UTC
    Hi, I don't do Tkx, but it is similar to Tk, which I am familiar with.

    I googled for "tkx fileevent" and came up with this code usage

    my $fh = Tkx::open("| @cmds <$null 2>\@1"); Tkx::fconfigure($fh, -blocking => 0); Tkx::fileevent($fh, readable => [\&run_watch, $fh]);
    It LOOKS like you need to pass in an arrayref, rather than a sub.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Moving forward, but not all the way.
      The $fh (filehandle) is not the same as $server. My error:
      can not find channel named "IO::Socket::INET=GLOB(0xa119ed0)" at ./spshow_simple.pl line 24.
      Offending sourcecode line: Tkx::fconfigure($server, -blocking => 0);

      Thank you
        I'm just guessing here, but you apparently still don't have the fileevent syntax correct. You still have
        Tkx::fileevent( $client, readable =>[\&handle_connection, \$client] ); # whereas the correct syntax I previously posted is Tkx::fileevent($fh, readable => [\&run_watch, $fh]);
        Notice, you have \$client as a reference instead of a plain filehandle

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
        FWIW, there is no need to resort to the Tcl blocking method, you can use $server->blocking(0);
      I googled for "tkx fileevent" and came up with this code usage
      ++ good show!
        That is right, but the example uses a file instead of a socket:
        my $fh = Tkx::open("| @cmds <$null 2>\@1");
Re: Tkx and tcp server
by Anonymous Monk on Mar 17, 2011 at 19:03 UTC
      Found some info, thank you. Updated my question with code.
Re: Tkx and tcp server
by Anonymous Monk on Mar 18, 2011 at 13:09 UTC
    Made server-code that might work (does not) and a client.