Tkx socket server can't read input

[email protected] (Kevin Walzer) Sun, 31 Mar 2013 18:52:24 -0400
Newsgroups perl.tcltk
Organization WordTech Communications LLC
Message-ID <[email protected]>
Hello,

I'm trying to implement a simple tcp server using Tkx, based on some 
sample code I've found on this mailing list and PerlMonks, and am having 
some trouble. The idea is that a client script will send some data to 
the server, and the server will print the data into its own display.

Here is the client script:

#!/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..5){
   print $sock "$count\n";
   print "$count\n";

}
close ($sock);


And the server script:

use strict;
use warnings;

use Tkx;
Tkx::package_require('tile');
my $mw = Tkx::widget->new(".");


my $server = Tkx::socket(-server => [\&new_connection], 7777);

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 $client = shift;
     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;
     my $n;
   # eval { $message = Tkx::gets($client, $n); };
      $message = Tkx::gets($client, $n);
     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();
     }
}

The server starts fine, but when I fire up the client script and send 
data, the server hangs with this error message:

can not find channel named "::perl::SCALAR(0x7fa3ab87cdc8)" at 
tkx-server.pl line 38.

can not find channel named "::perl::SCALAR(0x7fa3ab87cdc8)"
     while executing
"gets ::perl::SCALAR(0x7fa3ab87cdc8) {}"
     invoked from within
"::perl::CODE(0x7fa3ab85a878)"

The relevant line is in the hande_connection subroutine:

      $message = Tkx::gets($client, $n);

The server seems to be acknowledging the connection, so I'm not clear 
why it can't find a channel, nor display any data (1,2,3,4,5) sent over 
the wire.

I understand that on the server side, using normal Perl sockets won't 
work because they don't integrate with Tk's event loop, hence using Tcl 
sockets (exposed via Tkx) is necessary; I assume this isn't an issue on 
the client side. However, I'm unclear what's going on with the error. 
Can anyone illuminate me?

Thanks,
Kevin

-- 
Kevin Walzer
Code by Kevin/Mobile Code by Kevin
http://www.codebykevin.com
http://www.wtmobilesoftware.com

-- 
Kevin Walzer
Code by Kevin/Mobile Code by Kevin
http://www.codebykevin.com
http://www.wtmobilesoftware.com