Simple Tk script

D

deadpickle

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.
 
Z

zentara

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top