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; }
In reply to asynchronous socket communication with perl tk by simonz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |