<?xml version="1.0" encoding="windows-1252"?>
<node id="636518" title="joeface's scratchpad" created="2007-09-01 10:41:55" updated="2007-09-01 06:41:55">
<type id="182711">
scratchpad</type>
<author id="61400">
joeface</author>
<data>
<field name="doctext">
&lt;p&gt;
First journey into two new worlds of Perl.  Client/Server using IO::Socket, and Perl Tk because I got bored of using the CLI client on Windows...
&lt;/p&gt;

&lt;p&gt;
The server runs on a linux box, and the client runs on an adjacent windows box
&lt;/p&gt;

&lt;p&gt;
The client accepts a URL, and sends it off to the server, which receives it (along with a built-in "fetch" command), and uses wget to fetch whatever's at that location.
&lt;/p&gt;

&lt;p&gt;
Silly use, yes, but it had to do more than Hello World.  God only knows what kinds of ideas this is going to give me.  Opened up and began illuminating a few dark, scary rooms that I was previously locked out of.
&lt;/p&gt;


&lt;p&gt;
Evolved out of just sending back the string it was originally given, then sending back a reversed string.  That code is still in there, but the Tk client only sends the "fetch" command.
&lt;/p&gt;

&lt;p&gt;
Server:
&lt;code&gt;
#!/usr/bin/perl -w

use strict;
use IO::Socket;
use Getopt::Long;
use File::Basename;

my $scriptname = basename($0);
my $scriptdir  = dirname($0);

## Standard options
my $silent;
my $verbose;

GetOptions(
            "s"   =&gt; \$silent,
            "v"   =&gt; \$verbose,
           );



my $localhost = '192.168.0.100';
my $port = 55555;
my $save_loc = '/var/tmp/client-server-gets';

my $server = new IO::Socket::INET (
                                   LocalHost =&gt; $localhost,
                                   LocalPort =&gt; $port,
                                   Proto     =&gt; 'tcp',
                                   Listen    =&gt; 1,
                                   Reuse     =&gt; 1,
                                  );

$server or die "Can't create server: $!\n";

printinfo("$scriptname listening on port $port");


while(my $HANDLER = $server-&gt;accept()) {
    $HANDLER or die "server-&gt;accept: $!\n";

    my $peeraddr = $HANDLER-&gt;peeraddr;
    my $peerhost = $HANDLER-&gt;peerhost;
    my $hostinfo = gethostbyaddr($peeraddr, AF_INET);
    printinfo("Accepted connection from $peerhost");

    my $in = &lt;$HANDLER&gt;;
    chomp($in);
    if ($in) {

        printinfo("Client said \"$in\"");
        my ($cmd,) = split(/\s+/, $in);
        $in =~ s/.*?\s+//;
        if ($cmd eq 'repeat') {
            print $HANDLER localtime() . ": you said \"$in\"\n";
            printinfo("Sent \"$in\" to client");
        } elsif ($cmd eq 'reverse') {
            $in = reverse($in);
            print $HANDLER localtime() . ": you said \"$in\"\n";
            printinfo("Sent \"$in\" to client");
        } elsif ($cmd eq 'fetch') {
                my $file = basename($in);
            if ($verbose) {
                system("wget -N -P $save_loc $in");
            } else {
                system("wget -N -P $save_loc -q $in");
            }
            my $rc = $? &gt;&gt; 8;
            if ($rc == 0) {
                printinfo("Fetch of item \"$in\" successful");
                print $HANDLER "Requested item saved as $save_loc/$file\n";
            } else {
                printinfo("Fetch of item \"$in\" failed");
                print $HANDLER "Sorry, your request failed\n";
            }
        } else {
                print $HANDLER "Sorry, there was no known command attached to your msg\n";
        }
    }
}

sub printinfo {
    unless($silent) {
        print localtime() . ": @_\n";
    }
}

__END__
&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;
Client:
&lt;code&gt;
#!/usr/bin/perl -w
use Tk;
use strict;
use IO::Socket;
use File::Basename;

my $server = '192.168.0.100';
my $port = 55555;

my $mw = MainWindow-&gt;new;

$mw-&gt;Label(
           -text =&gt; "\nURL to desired file"
          )-&gt;pack;

my $url = $mw-&gt;Entry(
                     -width =&gt; 100
                    )-&gt;pack;


$mw-&gt;Label(-text =&gt; " ")-&gt;pack; ######################## SPACER

my $button = $mw-&gt;Button(
                -text    =&gt; 'Fetch',
                -command =&gt; sub{do_fetch($url)},
                -padx    =&gt; 10,
                        )-&gt;pack;
#$button-&gt;bind('&lt;Enter&gt;', sub{do_fetch($url)});

$mw-&gt;Label(-text =&gt; " ")-&gt;pack; ######################## SPACER

my $ent_msg;
my $ent = $mw-&gt;Entry(-width        =&gt; 100,
                     -state        =&gt; "readonly",
                     -textvariable =&gt; \$ent_msg)-&gt;pack();

MainLoop;

sub print_status {
    my $msg_txt = join(" ", @_);
    $ent_msg = $msg_txt;
    $ent-&gt;update();
}


################## CLIENT PORTION

sub do_fetch {

    print_status("Submitted request to $server");
    my $url = shift;
    my $url_val = $url-&gt;get;

    my $scriptname = basename($0);
    my $command = 'fetch';

    my $msg = $url_val;

    unless($msg) {
    	print "Usage: $scriptname &lt;msg&gt;\n";
	exit;
    }
    chomp($msg);


    my $client = new IO::Socket::INET (
                                       PeerAddr =&gt; $server,
                                       PeerPort =&gt; $port,
                                       Proto    =&gt; "tcp",
                                      )
    or die "Can't connect to $server:$port : $!\n";

    print $client $command . " " . $msg . "\n";

    my $response = &lt;$client&gt;;

    if ($response) {
        chomp($response);
        print_status("$response");
    }

}

__END__
&lt;/code&gt;
&lt;/p&gt;</field>
</data>
</node>
