Usage: init(class=NULL) at ./client.pl line 35.
####
Gtk2->init(\@ARGV);
####
#!/usr/bin/perl
# server1.pl - a simple server
use strict;
use Socket;
# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');
# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);
# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";
$| = 1;
# for each connection...
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER)) {
# find out who connected
my ($client_port, $client_ip) =
sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host =
gethostbyaddr($client_ip, AF_INET);
# tell who connected
print "got a connection from: $client_host",
" [$client_ipnum]\n";
# send them a message, close connection
my $w = `w`;
# log the access event
print CLIENT "$w\n ";
close CLIENT;
}
####
#! /usr/bin/perl
# client-gui.pl - a simple socket client GUI
use strict;
use Socket;
use Gtk2;
# initialize some variables
my $port = 7890; # hard coded port the server runs on
# we could have made this an argument instead
# used by the hbox, vbox and widget packing
my $homogenous = 0; # a "1" here would make all
# widgets the same height or width
my $spacing = 0; # spacing between widgets in pixels
my $expand = 1; # widgets expand to fit available space
my $fill = 0; # extra space used by widgets or empty
my $padding = 0; # no padding
# build the GUI
Gtk2->init(\@ARGV);
my $window = Gtk2::Window->new('toplevel');
$window->set_title("Network 'w' Monitor GUI");
$window->set_border_width(6);
$window->set_size_request(400, 180);
my $vbox = Gtk2::VBox->new($homogenous, $spacing);
my $hbox = Gtk2::HBox->new($homogenous, $spacing);
my $label_host = Gtk2::Label->new("Hostname to poll:");
my $entry_host = Gtk2::Entry->new;
my $text_result = Gtk2::TextView->new;
my $scrolled_window = Gtk2::ScrolledWindow->new(undef, undef);
$scrolled_window->set_policy('automatic', 'automatic');
$scrolled_window->add($text_result);
my $buffer = $text_result->get_buffer;
$text_result->set_editable(0);
my $button_poll = Gtk2::Button->new_with_label("Poll");
my $button_exit = Gtk2::Button->new_with_label("Exit");
$hbox->pack_start($label_host, $expand, $fill, $padding);
$hbox->pack_start($entry_host, $expand, $fill, $padding);
$vbox->pack_start($hbox, $expand, $fill, $padding);
$vbox->pack_start($scrolled_window, $expand, $fill, $padding);
$vbox->pack_start($button_poll, $expand, $fill, $padding);
$vbox->pack_start($button_exit, $expand, $fill, $padding);
Gtk2::GSignal->connect($button_poll,"clicked", \&poll_host);
Gtk2::GSignal->connect($button_exit,"clicked", \&exit_app);
$window->add($vbox);
$window->show_all();
# Gtk2 event loop
Gtk2->main();
exit 0;
# exit routine
sub exit_app {
Gtk2->quit();
return 0;
}
# poll routine
sub poll_host {
my $host = $entry_host->get_text;
if ($host eq '') {
gui_err("Empty Host Name!");
return;
}
my $buff = '';
my $proto = getprotobyname('tcp');
# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or gui_err("socket: $!");
connect(SOCKET, $paddr) or gui_err("connect: $!");
while () {
$buff .= $_;
}
close SOCKET or gui_err("close: $!");
$buffer->set_text($buff, -1) if ($buff ne '');
}
# error feedback
sub gui_err {
my $msg = shift;
$buffer->set_text($msg, -1);
}