http://www.perlmonks.org?node_id=636518

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

The server runs on a linux box, and the client runs on an adjacent windows box

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.

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.

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.

Server:

#!/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" => \$silent, "v" => \$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 => $localhost, LocalPort => $port, Proto => 'tcp', Listen => 1, Reuse => 1, ); $server or die "Can't create server: $!\n"; printinfo("$scriptname listening on port $port"); while(my $HANDLER = $server->accept()) { $HANDLER or die "server->accept: $!\n"; my $peeraddr = $HANDLER->peeraddr; my $peerhost = $HANDLER->peerhost; my $hostinfo = gethostbyaddr($peeraddr, AF_INET); printinfo("Accepted connection from $peerhost"); my $in = <$HANDLER>; 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 = $? >> 8; if ($rc == 0) { printinfo("Fetch of item \"$in\" successful"); print $HANDLER "Requested item saved as $save_loc/$fil +e\n"; } else { printinfo("Fetch of item \"$in\" failed"); print $HANDLER "Sorry, your request failed\n"; } } else { print $HANDLER "Sorry, there was no known command atta +ched to your msg\n"; } } } sub printinfo { unless($silent) { print localtime() . ": @_\n"; } } __END__

Client:

#!/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->new; $mw->Label( -text => "\nURL to desired file" )->pack; my $url = $mw->Entry( -width => 100 )->pack; $mw->Label(-text => " ")->pack; ######################## SPACER my $button = $mw->Button( -text => 'Fetch', -command => sub{do_fetch($url)}, -padx => 10, )->pack; #$button->bind('<Enter>', sub{do_fetch($url)}); $mw->Label(-text => " ")->pack; ######################## SPACER my $ent_msg; my $ent = $mw->Entry(-width => 100, -state => "readonly", -textvariable => \$ent_msg)->pack(); MainLoop; sub print_status { my $msg_txt = join(" ", @_); $ent_msg = $msg_txt; $ent->update(); } ################## CLIENT PORTION sub do_fetch { print_status("Submitted request to $server"); my $url = shift; my $url_val = $url->get; my $scriptname = basename($0); my $command = 'fetch'; my $msg = $url_val; unless($msg) { print "Usage: $scriptname <msg>\n"; exit; } chomp($msg); my $client = new IO::Socket::INET ( PeerAddr => $server, PeerPort => $port, Proto => "tcp", ) or die "Can't connect to $server:$port : $!\n"; print $client $command . " " . $msg . "\n"; my $response = <$client>; if ($response) { chomp($response); print_status("$response"); } } __END__