simonz has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I am including a code below which demonstrates a simple client-server communication in a perl tk window. How can I achieve this communication when multiple clients are trying to interact at a time.
client codeuse Tk; use IO::Socket; my $status = ''; my $mw = MainWindow->new; $mw->geometry("200x200"); my $buttonFrame = $mw->Frame()->pack(-side => 'bottom'); $buttonFrame->Button(-text => 'Start', -command => sub { init_server(\$mw, \$status) } )->pack(-side => 'left'); my $displayFrame = $mw->Frame()->pack(-side => 'top'); $displayFrame->Label(-text => 'Status:')->pack(-side => 'left'); $displayFrame->Label(-textvariable => \$status)->pack(-side => 'left') +; MainLoop; sub init_server { my ($mw_ref, $status_ref) = @_; my $server = IO::Socket::INET::->new( Proto => 'tcp', LocalPort => 55555, Listen => 1, Reuse => 1 ) or die "Server can't start: $!"; my $client = $server->accept(); $client->autoflush; $$mw_ref->fileevent($client, 'readable', sub { if (defined(my $read = <$client>)) { chomp $read; $$status_ref = $read; } }); }
use IO::Socket; my $client = IO::Socket::INET::->new( Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 55555 ) or die "Client can't connect: $!"; my @msgs = qw(msg1 msg2 msg3 msg4 msg5); for (@msgs) { print $client "$_\n"; sleep 1; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: asynchronous socket communication with perl tk
by kcott (Archbishop) on Sep 25, 2013 at 13:18 UTC | |
by simonz (Sexton) on Sep 26, 2013 at 14:23 UTC | |
by simonz (Sexton) on Sep 27, 2013 at 08:56 UTC |
Back to
Seekers of Perl Wisdom