Simple Tk script

Discussion in 'Perl Misc' started by deadpickle, Dec 18, 2006.

  1. deadpickle

    deadpickle Guest

    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.
    deadpickle, Dec 18, 2006
    #1
    1. Advertising

  2. deadpickle

    zentara Guest

    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
    zentara, Dec 19, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. geoffbache
    Replies:
    5
    Views:
    895
    geoffbache
    Jun 13, 2008
  2. Rajat
    Replies:
    3
    Views:
    680
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    544
    Jan Kaliszewski
    Jan 7, 2010
  4. dillon
    Replies:
    1
    Views:
    170
    higabe
    Nov 17, 2003
  5. Wladimir Borsov
    Replies:
    4
    Views:
    285
    Tintin
    May 11, 2006
Loading...

Share This Page