momo33 has asked for the wisdom of the Perl Monks concerning the following question:
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...)
Thank you#!/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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tkx and tcp server
by zentara (Cardinal) on Mar 18, 2011 at 15:05 UTC | |
by momo33 (Beadle) on Mar 18, 2011 at 15:40 UTC | |
by zentara (Cardinal) on Mar 21, 2011 at 14:59 UTC | |
by Anonymous Monk on Mar 21, 2011 at 16:49 UTC | |
by Anonymous Monk on Mar 19, 2011 at 03:46 UTC | |
by momo33 (Beadle) on Mar 19, 2011 at 07:45 UTC | |
|
Re: Tkx and tcp server
by Anonymous Monk on Mar 17, 2011 at 19:03 UTC | |
by momo33 (Beadle) on Mar 18, 2011 at 06:15 UTC | |
|
Re: Tkx and tcp server
by Anonymous Monk on Mar 18, 2011 at 13:09 UTC |