Re: Tkx socket server can't read input

[email protected] (Kevin Walzer) Tue, 02 Apr 2013 18:32:01 -0400
Newsgroups perl.tcltk
Organization WordTech Communications LLC
Message-ID <[email protected]>
Hi Vadim,

I've edited my server script along the lines you suggest, removing the 
"update" references (because it causes Tk on my Mac to go into an 
infinite loop):

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::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;
     $message = Tkx::gets($client);
     if ( defined $message and $message !~ /^quit/ ) {
         $message =~ s/[\r\n]+$//;
         $log->insert( 'end', "$message\n" );
         $log->see('end');
     }
     else {
         print "connection closed\n";
         $log->insert( 'end', "connection closed\n" );
         $log->see('end');
         $client->close();
     }
}

And here's my client script again:

#!/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);

This doesn't display any output at all. When I add the "print $sock 
'quit\n'" line to the client script, it prints this:

connected
1
connection closed

Either way, this isn't work the way I expect. Do you have any 
suggestions as to what I may be doing wrong?

--Kevin

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