#!/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/$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__ #### #!/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('', 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 \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__