Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Server with GUI

by gg4000 (Novice)
on May 27, 2012 at 13:02 UTC ( [id://972697]=perlquestion: print w/replies, xml ) Need Help??

gg4000 has asked for the wisdom of the Perl Monks concerning the following question:

Hello I'm close. The problem is that the GUI will not open until the server gets a connection. The GUI should open first showing the IP of the computer, ready to receive commands, and show the command. Please help! Here's the code.....

#!/usr/bin/perl<br> package main;<br> use IO::Socket;<br> use Sys::Hostname;<br> use Socket;<br> use 5.008;<br> #use strict;<br> #use warnings;<br> my($ipaddr)=inet_ntoa((gethostbyname(hostname))[4]);<br> $| = 1;<br> my $app = Demo::App->new;<br> # create the WxApplication<br> #$self->SetSize($text->GetSizeWH);<br> $app->MainLoop;<br> package Demo::App;<br> #use strict;<br> #use warnings;<br> use base 'Wx::App';<br> sub OnInit {<br> my $frame = Demo::App::Frame->new;<br> $frame->Show(1);<br> }<br> package Demo::App::Frame;<br> #use strict;<br> #use warnings;<br> use Wx qw(:everything);<br> use base 'Wx::Frame';<br> sub new {<br> my ($class) = @_;<br> my $self = $class->SUPER::new(<br> undef, -1,<br> 'Demo::App',<br> wxDefaultPosition, wxDefaultSize,<br> );<br> my $text = Wx::StaticText->new( $self, -1, "For X10 Voice Commande +r Use IP $ipaddr\n Port 8086\n");<br> #sub serv {<br> $local = IO::Socket::INET->new(<br> Proto => 'tcp', # protocol<br> LocalAddr => "$ipaddr:8086", <br> ) or die "$!";<br> my $addr; <br> $local->listen(); <br> $local->autoflush(1); <br> while ($addr = $local->accept() ) { <br> print "Connection from: ", $addr->peerhost(),"\n"; <br> while (<$addr>) <br> { <br> $path = "C:/Program Files (x86)/Common Files/X10/Common";<br> chdir($path) or die "Cant chdir to $path $!";<br> ~s/GET//,~s/~/ /g,~s/%20/ /g,~s/%22/ /g,~s/x10command=DEVICE/ +/,~s/\//\ /g,~s/[?]//g ,~s/'/ /g,~s/HTTP/ /,~s/1.1/ /g,~s/sh://;<br> system(AHCMD. "$_");<br> print "Received: $_"; <br> print $addr $_; <br> print $path $_; <br> close $addr;<br> chomp;<br> return $self; <br> } <br> }<br> }<br>

Replies are listed 'Best First'.
Re: Server with GUI
by zentara (Archbishop) on May 27, 2012 at 13:37 UTC
    I don't use Wx, but you cannot run network connection code, within your GUI setup code blocks. You have the network connection code, right within the package Demo::App::Frame. So it must do it thing, before the GUI shows.

    As a first start at a solution, take the network connection code out of that package, put it in a subroutine, and make a "Connect" button to launch it. That way, your GUI will come up first, then you press the Connect button. Also be warned that you will probably need to put your network handling code in a separate thread, so the network action won't interfere with the Wx event loop.

    After you get it working that way, you probably can use a Wx method to test for "visibility", before lauching your network sub with a timer.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      you cannot run network connection code, within your GUI setup code blocks. You have the network connection code, right within the package Demo::App::Frame. So it must do it thing, before the GUI shows.

      I tried moving it to the top. I still can't get them both to happen.

Re: Server with GUI
by ww (Archbishop) on May 27, 2012 at 21:22 UTC

    If I uncomment the very first instance of "use strict; use warnings" 5.014 coughs up an error:

    Number found where operator expected at D:\_Perl_\pl_test\972697.pl line 10, near ")4".

    I'd tell you the line numbers but you ignored the bold face instruction to use <code>...</code> tags around your code (and, in the case of the error) quoted above, we're off by one because I assign the node number as the title of scripts I want to troubleshoot/help with/diddle and include it on its own line between the hashbang and the body of the code.

    But those quibbles notwithstanding, my subsequent attempts to reformat your script (to something I find easy to follow) failed, perhaps because I couldn't figure out how to deal with the way you've commented out the declaration of #sub serv {.

    Perhaps you'd like to revisit this -- and either update the OP or post the actual code that you think is "close" inside code tags.

    Remember, if you make it easy for the Monks to grok your code and your intent, you're more likely to get solid answers.

      Hello again.

      I am very new to Perl. I've been working on making this server for a couple of weeks. I've tried VB, Java, PY. Perl seems to be working so far. I do have it working in a black box (CMD). I've commented out, moved } curlys, etc for testing.

      My plans are to have a window that shows the IP of the computer, and text from the client, and the time/date the client connected. Once I get that working, I will be working on having the user input for a port number. Then save the configuration. It looks like that is far away since I can't seem to even get the GUI to work.

      Right now I will be happy with the label showing the IP and the text from the client showing in the text box.

      Is it a gosub, goto thing I'm missing. Please point me in the right direction.
Re: Server with GUI
by zentara (Archbishop) on May 28, 2012 at 16:40 UTC
    Please point me in the right direction.

    Since you are new to Perl, I will give you some tips. In any GUI, if you run a while loop( or sleep ) as you do in your code, you will interfere with the GUI's eventloop, which is the line $app->MainLoop; That will make the GUI freeze unless you manually update the mainwindow everytime thru the loop. You just can't mix conventional Perl code with GUI code, and expect it to run.

    You can run a network while loop with a GUI front end, but the network loop must be in a separate thread. This however, is generally not a problem with GUI's because almost every GUI toolkit has a fileevent or IO watch method, which allows the GUI to watch the network without a while loop. I googled for it, and was surprised to read that Wxwidgets has no filehandle watch. See AnyEvent for a workaround. Maybe a Wx guru knows a better way.

    To show how it should work, using Tk , see Re: hanged In Tk-socket program. In that server, I make the socket before the GUI pops up, and use a fileevent to handle incoming connections. This is probably the simplest example possible.

    You might want to switch to Tk if you don't understand how Wx or GUI's work. For more complex Tk server-client pairs, see ztk-enchat encrypted server client and Tk encrypted echoing-chat client and server

    Once you get the idea of how it is supposed to work with Tk, then you can go back and try to do the same thing with Wx. Sockets are not intuitively obvious to make run correctly.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you for the quick reply.

      I do have a script the same as this using Tk. It has been doing the same thing as Wx. I will do what you suggested and use Tk.

      Tk will not install on my desktop, but will on my laptop. I'd love to see the fireworks on the desktop! So I've been doing the work on the desktop, then email it to the laptop to test.
        Tk will not install on my desktop

        If you post what operating system you have, and OS level, someone may be able to help you get Tk on your Desktop. I'm assuming it's Windows?


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
      I Got FireWorks!!! : ) Last night I did a fresh install of Strawberry. Today Tk was installed. I tried the Re: hanged In Tk-socket program, but I did not see the typed text from the client to the server. Did I do something wrong? I typed Hello in the client, hit enter with the server running, but nothing happened.
        I did not see the typed text from the client to the server.

        You may have a Window's related problem, where Tk's fileevent dosn't work reliably on some version of windows. See Client/Server sockets with TK on Win32 .

        There are workarounds, see Another Win32 Tk fileevent work around : using ioctl() properly for instance. If that dosn't work for you, it's possible to use a conventional select loop with sysread in a separate subroutine, and call it with a Tk timer every 10 milliseconds. See Tk fileevent with win32 sockets for a timer solution.

        It goes something like this untested code:

        ... other Tk code my $timer = $mw->repeat(10, \&read_it ); MainLoop; sub read_it{ my $client = $listen->accept or die "Accept failed: $!"; if( IO::Select->new($client)->can_read(5) ) { sysread( $client, $_, 1 ) and print; } else { print "Timeout\n"; } close $client; }

        Also see Mastering Perl/Tk and the Window's fileevent problem

        If you are an eager seeker of Perl knowledge, I will tell you that another, more advanced GUI toolkit, called Gtk2 does have an IO watch ( like fileevent ) which does work on Windows, see Simple threaded chat server for a simple example to test with.

        Finally, you could just put Ubuntu Linux on your Desktop with a dual boot to Windows. :-)

        Finally, just to get you banging your head against the wall, I found an example of Wx using sockets, see Wx Socket. It probably works on Windows.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
        Tk does work with the command prompt. Tk will not work with Padre.

        Can't locate Tk.pm in @INC (@INC contains: C:/Dwimperl/perl/site/lib C:/Dwimperl /perl/vendor/lib C:/Dwimperl/perl/lib .) at monkserver.pl line 5.

        Seems like it should be easy to fix, but I don't know what a @INC is.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://972697]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-23 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found