Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

asynchronous socket comunication with perl tk

by ghosh123 (Monk)
on Aug 22, 2013 at 10:01 UTC ( #1050500=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

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.

Comment on asynchronous socket comunication with perl tk
Re: asynchronous socket comunication with perl tk
by choroba (Abbot) on Aug 22, 2013 at 10:11 UTC
    Have you tried searching for socket Tk?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: asynchronous socket comunication with perl tk
by kcott (Abbot) on Aug 22, 2013 at 16:35 UTC

    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:

    • The socket-related code came (mostly verbatim) from perlipc. See the TCP Clients with IO::Socket and TCP Servers with IO::Socket sections if you have questions with any of that.
    • See Tk::fileevent for questions about the file event handler.
    • See Tk for links to any of the other GUI code.
    • There's no requirement to run either the server or client in the background: that's just my preference as it frees up the command-line for other work.

    -- Ken

      Thanks a lot Ken .
      Well to give you some hint of how I want the gui to be :

      1. There will be a 'load' button on it which will load for example two jobs. Job1 and Job2 under columns Jobname and Status.
      2. Primarily those job1 and job2 status should be Stopped.
      3. There will be one more button Start, clicking start button those two jobs should start running in the background.
      4. Those jobs will be simple perl file with just a sleep statement for 20s and 10s respectively.
      5. As long as those jobs will be running , the job status under status column should change to 'Running' from 'Stopped'. And as the jobs finished their status will be changed to 'Finished'.
      6. The gui should not know anything about what job is running, but through socket it should get the information and display its status accordingly.
      7. After clicking 'start' button, the text on it should change to 'stop'. And then pressing 'stop' it should toggle back to 'start' and accordingly the job status should also change to 'Stopped' from 'Running'.

      I have little modified your code to explain my wish. I have implemented #1 and #2 mentioned above. But need help on #3 to #7.

      #!/usr/bin/perl use strict; use warnings; use Tk; use IO::Socket; use Tk::HList; my $status = ''; my $mw = MainWindow->new; $mw->geometry("200x100"); 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 => 'Load', -command => sub { load_job() } )->pack(-side => 'left'); $control_F->Button(-text => 'start', -command => sub { } )->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'); my $hlist = $status_F->Scrolled( 'HList', -scrollbars => 'ose', -columns => 2, -header => 1, -height => 5, )->pack( -fill => 'both', -expand => 1 ); my $label1 = $hlist->Label( -text => "Jobname", -anchor => 'w' ); $hlist->headerCreate( 0, -itemtype => 'window', -widget => $label1 ); my $label2 = $hlist->Label( -text => "Result", -anchor => 'w' ); $hlist->headerCreate( 1, -itemtype => 'window', -widget => $label2 ); sub load_job { $hlist->add(0); $hlist->itemCreate(0,0,-text=> "job1"); $hlist->itemCreate(0,1,-text=> "stopped"); $hlist->add(1); $hlist->itemCreate(1,0,-text=> "job2"); $hlist->itemCreate(1,1,-text=> "stopped"); } 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; } }); }
        "...
        3. There will be one more button Start, clicking start button those two jobs should start running in the background.
        ...
        7. After clicking 'start' button, the text on it should change to 'stop'. And then pressing 'stop' it should toggle back to 'start' and accordingly the job status should also change to 'Stopped' from 'Running'.
        ... need help on #3 to #7."

        I provided code showing button creation, callbacks and changing widget text. I also provided documentation links.

        What exactly do you need help on?

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050500]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-18 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls