Specifications
Writing Server Agents
Equalizer Installation and Administration Guide 163
.
Here is the output of the server program when it is started on the server:
$ ./serveragent.pl 50
Server agent started on port 1510
Connection from: [10.0.0.32]
Here is what I see when I telnet to the port:
$ telnet 10.0.0.120 1510
50
Connection to host lost.
#!/usr/bin/perl -w
# serveragent.pl
#--------------------
#(c) Copyright 2007 Coyote Point Systems, Inc.
use strict;
use Socket;
# use port 1510 as default my $port = 1510;
my $proto = getprotobyname('tcp');
# take the response from the command line
my $response = shift;
# response has to be a valid server agent response
response==-1 or ($response > 0 and $response<101)
or die "Response must be between -1 and 100";
# create a socket and set the options, set up listen port
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
my $paddr = sockaddr_in($port, INADDR_ANY);
# bind to the port, then listen on it
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "Server agent started on port $port\n";
# accepting a 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);
# print who has connected -- this is for debugging only
print "Connection from: [$client_ipnum]\n";
# send them a message, close connection
print CLIENT $response;
close CLIENT;
}