Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Server with GUI

by zentara (Archbishop)
on May 30, 2012 at 10:40 UTC ( [id://973254]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Server with GUI
in thread Server with GUI

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

Replies are listed 'Best First'.
Re^4: Server with GUI
by gg4000 (Novice) on May 30, 2012 at 16:16 UTC
    LOL!!! Finally, just to get you banging your head against the wall, I found an example of Wx using sockets,

    Thanks for all the info. I will play with it when I get home from work.

Re^4: Server with GUI
by gg4000 (Novice) on Jun 01, 2012 at 23:46 UTC
    Well it's been a long week of trial and error.

    Had things happen with Tk, had things happen with Wx.

    I'm back to the start. Wx.

    The window opens after the server gets a connection. The Window has to open and display the IP First.

    Then display the text from the client. And display the next set of text from the client.

    And send the commands to the computer. I'm still stuck with "What to do instead of While"

    #!/usr/bin/perl package main; use IO::Socket; use Sys::Hostname; use Socket; use 5.008; #use strict; use Wx; use wxPerl::Constructors; use base 'Wx::App'; use Wx qw(wxTE_MULTILINE wxVERTICAL wxID_DEFAULT); #use warnings; my($ipaddr)=inet_ntoa((gethostbyname(hostname))[4]); $| = 1; my $app = Demo::App->new; $app->MainLoop; package Demo::App; #use strict; #use warnings; use base 'Wx::App'; sub OnInit { my $frame = Demo::App::Frame->new; $frame->Show(1); } package Demo::App::Frame; #use strict; #use warnings; use Wx qw(:everything); use base 'Wx::Frame'; sub new { #print "Ready for next command.\n\n"; my ($class) = @_; my $self = $class->SUPER::new( undef, -1, "X10 Voice Commander", wxDefaultPosition, wxDefaultSize, ); $self->{text} = Wx::TextCtrl->new($self, -1, "", [-1,-1], [300, 300], +wxTE_MULTILINE); $self->{text}->AppendText("Use Your IP $ipaddr:8087 on your devi +ce.\n"); #$self->{text}->AppendText("Connection from: ", $addr->peerhost(), +"\n"); #sub serv { $local = IO::Socket::INET->new( Proto => 'tcp', # protocol LocalAddr => "$ipaddr:8087", ) or die "$!"; my $addr; $local->listen(); $local->autoflush(1); while ($addr = $local->accept() ) { print "Connection from: ", $addr->peerhost(),"\n"; while (<$addr>) { $path = "C:/Program Files (x86)/Common Files/X10/Common"; chdir($path) or die "Cant chdir to $path $!"; ~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://; system(AHCMD. "$_"); print "Received: $_"; print $addr $_; print $path $_; close $addr; chomp; return $self; } } }

    And a test client.....
    use IO::Socket; use Sys::Hostname; my($ipaddr)=inet_ntoa((gethostbyname(hostname))[4]); $remote = IO::Socket::INET->new( Proto => 'tcp', # protocol PeerAddr=> "$ipaddr:8087", PeerPort=> "8087", # port of server Reuse => 1, ) or die "$!"; print "Connected to ", $remote->peerhost, # Info message " on port: ", $remote->peerport, "\n"; $remote->autoflush(1); # Send immediately while (<>) { # Get input from STDIN print $remote " "; print $remote "sendplc "; print $remote $_; # Send to Server #last if m/^end/gi; # If 'end' then exit loop my $line = <$remote>; # Receive echo from server if ($line ne $_) { # If not the same as input #print "Error in sending output\n"; # error exit; # Terminate } print "End of client\n"; # End of client close $remote; # Close socket exit }
    Run the server first. Type anything into the client. Commands are if you have X10 installed "A1 On"
      Have you seen Gtk2 server and client GUI's with root messaging? :-) It should work on Windows. Wx generally uses Gtk libs as a basis, so if you have Wx installed you can probably install Gtk2. See Installing Gtk2 using PPM on Windows and Install GTK2 for example.

      But back to Wx:

      I don't have Wx running, but looking at your server code, I see you did not get the previous point I was making about GUI eventloop code, and using sleep, system, while loops, select loops, or anything which interferes with the eventloop. Your code , as written, will just hang in the while loop, making the Wx eventloop freeze up. I don't even have to run it to see that.

      You must use Wx::Socket to run sockets in Wx. Otherwise, you need to put all your while loops into a separate thread, and at your level of programming experience, I doubt you are up to that hassle. Go back and rewrite your code using the example code in Wx::Socket


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        I tried many different ways to install Gtk2. It just won't install. I'll keep trying though.

        I'll keep trying different things with Tk and Wx

        I was also trying to figure out how to reform the server without the "while". I need the part after the While to complete the next step.
        while ($addr = $local->accept() ) { print "Connection from: ", $addr->peerhost(),"\n";
        I don't know what I can slip in instead of While.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://973254]
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: (9)
As of 2024-04-23 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found