Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple Tk script

Reply
Thread Tools

Simple Tk script

 
 
deadpickle
Guest
Posts: n/a
 
      12-18-2006
What I want to do is create an interface using Tk. What I have so far
is:
use Tk;
use Tk::LabEntry;
use IO::Socket;
#Main window interface
my $mainwindow = new MainWindow;
my $left = $mainwindow->Frame->grid(-row => 1,
-column => 0,
-sticky => 'nw');
my $bottom = $mainwindow->Frame->grid(-row => 2,
-column => 0,
-columnspan => 3,
-sticky => 'nw');
$left->LabEntry(-label => "PORT",
-labelPack => [-side => "left", -anchor => "w" ],
-textvariable => \$port->{PORT},
-width => 5,
)->pack;
MainLoop;

The program this is for sets up a server that listens for a client to
connect. I have a few questions:

1. How do I set this up so that 'use strict' will work?
2. I want to be able to enter a value into the "PORT" widget that is
then stored in the variable $port, am I doing this correctly?
3. I want to create a text box at the bottm that will echo what port is
being used and whether or not a client has connected, any hints on how
to do this would be valueble?
4. When it is ready I want to be able to hit a button on the bottom
that runs the script until the button is hit again, then the script
stops. Here is the script I want to run:

my $sock = new IO::Socket::INET(
LocalPort => $port,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
$|=1;
my($new_sock, $c_addr, $buf);
while (($new_sock, $c_addr) = $sock->accept()) {
my ($client_port, $c_ip) = sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host = gethostbyaddr($c_ip, AF_INET);
print "got a connection from: $client_host","
[$client_ipnum]\n";
while (defined ($buf = <$new_sock>)) {
chomp($buf);
if ($buf=~/^FILE\|/){
my $filename = (split /\|/, $buf)[1];
unless (open (FH, "<", $filename)){
print $new_sock "NACK|File '$filename'
do not exists\n";
}
else {
while (my $line =<FH>){
chomp($line);
print $new_sock
"FIL|".$line."\n";
#
# Remove/comment next line in
production
#
print "SENT>FIL|".$line."\n";
}
close(FH);
print $new_sock "EOT|\n";
}
last;
}
elsif ($buf=~/^QUIT\|/){
last;
}
else {
print $new_sock "NACK|Command not
understood\n";
last;
}
}
close($new_sock);
}
exit;

Any help is appreciated. Thanks.

 
Reply With Quote
 
 
 
 
zentara
Guest
Posts: n/a
 
      12-19-2006
On 18 Dec 2006 14:57:13 -0800, "deadpickle" <>
wrote:

>What I want to do is create an interface using Tk. What I have so far
>is:


>The program this is for sets up a server that listens for a client to
>connect. I have a few questions:

I don't feel like rewriting your script for you, and that is what
I would have to do, to do it right. So I will just point out some
basics, and point you to an existing example which I previously have
written.
>
>1. How do I set this up so that 'use strict' will work?

use strict;
#then put 'my' in front of your variables

>2. I want to be able to enter a value into the "PORT" widget that is
>then stored in the variable $port, am I doing this correctly?

# declare and set default port , then use $port in your socket setup
my $port = 2230;
my $port_entry = $left->LabEntry(
-label => "PORT",
-labelPack => [ -side => "left", -anchor => "w" ],
-textvariable => \$port,
-width => 5,
)->pack;


>3. I want to create a text box at the bottm that will echo what port is
>being used and whether or not a client has connected, any hints on how
>to do this would be valueble?


Well you read the socket, then enter what is read into the text widget.
Use fileevent to read the socket.

>4. When it is ready I want to be able to hit a button on the bottom
>that runs the script until the button is hit again, then the script
>stops. Here is the script I want to run:


Make a button that will launch your socket code when first pressed,
then alter it's callback so it will do a $socket->close on the second
press.

>>Any help is appreciated. Thanks.


I'm not entirely sure what you are trying to do with this script.
Here is a similar script, that opens a socket( on startup without
a button) and collects data. It has a label at the bottom to show
when the client is connected or not.

See:
http://perlmonks.org?node_id=577181



--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
RE: How to execute a script from another script and other script doesnotdo busy wait. VYAS ASHISH M-NTB837 Python 2 01-07-2010 08:18 PM
Converting a simple python script to a simple windows executable geoffbache Python 5 06-13-2008 08:51 PM
How to call a simple perl script from HTML without need of HTTPS but simple HTTP ? Wladimir Borsov Perl Misc 4 05-11-2006 09:29 AM
Simple problem with a simple chat script dillon Javascript 1 11-17-2003 11:11 PM



Advertisments