G'day ghosh123,

"Can anybody please give me (or point me to) an example of asynchronous socket communication happening through a perl tk application. A very simple and primitive one.
The gui should be a server which will open a listening socket and some other processing application should connect to it and send info. The gui status should change according to the message received."

This is simple and primitive. :-)

The server (pm_asynch_sock_server_tk.pl):

#!/usr/bin/env perl use strict; use warnings; use Tk; use IO::Socket; my $status = ''; my $mw = MainWindow->new; my $control_F = $mw->Frame()->pack(-side => 'bottom'); $control_F->Button(-text => 'Listen', -command => sub { start_server(\$mw, \$status) } )->pack(-side => 'left'); $control_F->Button(-text => 'Quit', -command => sub { exit } )->pack(-side => 'left'); my $status_F = $mw->Frame()->pack(-side => 'top'); $status_F->Label(-text => 'Status:')->pack(-side => 'left'); $status_F->Label(-textvariable => \$status)->pack(-side => 'left'); MainLoop; sub start_server { my ($mw_ref, $status_ref) = @_; my $server = IO::Socket::INET::->new( Proto => 'tcp', LocalPort => 55555, Listen => SOMAXCONN, 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; } }); }

The client (pm_asynch_sock_client.pl):

#!/usr/bin/env perl use strict; use warnings; use IO::Socket; my $client = IO::Socket::INET::->new( Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 55555 ) or die "Client can't connect: $!"; my @msgs = 'A' .. 'Z'; for (@msgs) { print $client "$_\n"; sleep 1; }

To run, start the GUI (I ran it in the background):

$ pm_asynch_sock_server_tk.pl &

Then start the server (click the Listen button) followed by the client (again, I ran this in the background):

$ pm_asynch_sock_client.pl &

You'll see the letters of the alphabet appear one at a time every second as the Status: value. That's just my test: you probably want something entirely different but gave no hint as to what that might be.

Notes:

-- Ken


In reply to Re: asynchronous socket comunication with perl tk by kcott
in thread asynchronous socket comunication with perl tk by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.