Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

tk mainwindow not appearing

by simonz (Sexton)
on Oct 03, 2013 at 09:29 UTC ( #1056740=perlquestion: print w/ replies, xml ) Need Help??
simonz has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have written a per tk script, which first create a mainwindow and then starts a server program and client program. But the problem is if I shift the 'MainLoop' statement in the end of the script after the initiation of server and client , then the gui is not appearing although the server is getting started.
On the other hand if I put the 'MainLoop' before the server and client initiation statements then the gui is starting , but the execution control only goes to the server-client statement only after closing the gui. All I want is that the gui should start and simultaneously the server and client should also start in sequence.
For the server and client code you can refer to my code pasted in How to introduce threading in socket communication. Similar code is getting executed in Server::initServer() and Client::initClient() routines respectively.

## my script ##

use Tk; use Server; use Client; my $mw = MainWindow->new(); #MainLoop; #gui appears but the control goes below only after clos +ing the gui,hence server does not start when the gui is open print "after mainloop \n"; my $server = Server->new(parentWnd=>$mw); $server->initServer(); my $client = Client->new(parentWnd=>$mw); $client->initClient(); MainLoop; #does not bring up the gui but the server starts.

Comment on tk mainwindow not appearing
Download Code
Re: tk mainwindow not appearing
by Anonymous Monk on Oct 03, 2013 at 09:40 UTC

      I think I have clearly mentioned what the client and server program mean. The server starts and wait for the client to connect to it and send data. I will process that sent data from client and display that in gui. But the gui is not starting.
      Can you please point me what mistake I am making.

Re: tk mainwindow not appearing
by keszler (Priest) on Oct 03, 2013 at 10:56 UTC
    You say
    Similar code (to 1056724) is getting executed in Server::initServer() and Client::initClient() routines respectively.
    And
    But the problem is if I shift the 'MainLoop' statement in the end of the script after the initiation of server and client , then the gui is not appearing although the server is getting started.
    Without a listing of Server.pm and Client.pm my only guess is that $server->initServer(); or $client->initClient(); is not returning. Probably the former as you don't mention the client getting started.

      Hi keszler
      # The Server.pm ##

      package Server; use IO::Socket; use IO::Select; use strict; sub new { my $class = shift; my %args = @_; my $self = { parentWnd => $args{parentWnd} }; bless $self, $class; return $self; } sub initServer { my $self = shift; my $server = IO::Socket::INET::->new(Proto => 'tcp', LocalPort => 55555, Listen => 1, Reuse => 1 ) or die "Server can't start: $!" +; my $readable_handles = new IO::Select(); $readable_handles->add($server); my $buf; while (1) { # select() blocks until a socket is ready to be read or written my ($new_readable) = IO::Select->select($readable_handles, undef, undef, 0); # If it comes here, there is at least one handle # to read from or write to. For the moment, worry only about # the read side. foreach my $sock (@$new_readable) { if ($sock == $server) { my $new_sock = $sock->accept(); # Add it to the list, and go back to select because the # new socket may not be readable yet. print "Adding it \n"; $readable_handles->add($new_sock); } #- server part else { #print STDERR "Reading...\n"; print "Reading...$sock \n"; # It is an ordinary client socket, ready for reading. $buf = <$sock>; if ($buf) { #- print the buffer print "Read $buf\n"; # .... Do stuff with $buf } else { # Client closed socket. We do the same here, and remove # it from the readable_handles list print "removing it \n"; $readable_handles->remove($sock); close($sock); } } } } } 1;
      ## the Client.pm ###
      package Client; use IO::Socket; sub new { my $class = shift; my %args = @_; my $self = { parentWnd => $args{parentWnd} }; bless $self, $class; return $self; } sub initClient { my $self = shift; my $client = IO::Socket::INET::->new( Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 55555 ) or die "Client can't connect: $!"; my @msgs = 1 .. 10; for (@msgs) { print $client "$_\n"; sleep 1; } } 1;
        The initServer subroutine's while loop has no exit; your main program never gets past the $server->initServer(); line. You'll need to make Server and Client fork a child process to run thosethat endless loop, or look into IPC::Run, Thread::Queue, or the many links above in the AnonyMonk reply.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-12-28 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls