Perl Notes/IO::Socket::INET client server: Difference between revisions
From Federal Burro of Information
Jump to navigationJump to search
(New page: client <pre> server <pre> #!/usr/bin/perl -w use strict; use IO::Socket; require "/usr/local/perl/security/initdb2.pl"; require "/usr/local/perl/common/common2.pl"; require "/usr/local/...) |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
'''Client''' | |||
<pre> | <pre> | ||
server | #!/usr/bin/perl -w | ||
use strict; | |||
use IO::Socket; | |||
# initialize host and port | |||
my $port = 7890; | |||
my $server = "1.2.3.3"; | |||
my $verbose = 1; | |||
my $sock = new IO::Socket::INET ( | |||
PeerAddr => $server, | |||
PeerPort => $port, | |||
Proto => 'tcp'); | |||
die "Could not create socket: $!\n" unless $sock; | |||
$now = time; | |||
$verbose && print "$now Socket created\n"; | |||
my $Now = 0; | |||
my $Tower = 56; | |||
my $ServerNum = 3; | |||
my $Range = 0; | |||
my $line; | |||
eval { $line = <$sock>; }; | |||
if ( $@) { die "$now Failed to read first line\n"; } | |||
$now = time; | |||
if ( ! $line =~ /HELO/ ) { | |||
die "$now Server not ready\n"; | |||
} else { | |||
$now = time; | |||
print "$now got HELO\n" | |||
} | |||
print $sock "ShowGraph $Now:$Tower:$ServerNum:$Range:ScanQueue:Scan Queue Length:average\n"; | |||
$now = time; | |||
$verbose && print "$now Sent Show graph\n"; | |||
eval { $line = <$sock>; }; | |||
if ( $@) { die "$now Failed to get response to Showgreaph\n" } | |||
$now = time; | |||
if ( $line =~ /OK/ ) { | |||
print "$now Got OK: " .$line; | |||
} else { | |||
print "$now Error: $line \n"; | |||
} | |||
$now = time; | |||
print "$now Sending quit\n"; | |||
print $sock "QUIT\n"; | |||
close($sock) or die "close: $!"; | |||
</pre> | |||
'''Server''' | |||
<pre> | <pre> | ||
#!/usr/bin/perl -w | #!/usr/bin/perl -w | ||
Line 7: | Line 67: | ||
use strict; | use strict; | ||
use IO::Socket; | use IO::Socket; | ||
my $port = 7890; | my $port = 7890; | ||
Line 19: | Line 75: | ||
my $server = new IO::Socket::INET ( | my $server = new IO::Socket::INET ( | ||
LocalHost => ' | LocalHost => '1.2.3.4', | ||
LocalPort => $port, | LocalPort => $port, | ||
Proto => $proto, | Proto => $proto, | ||
Line 69: | Line 125: | ||
$verbose && print STDOUT "$now RECV QUIT command, QUITING\n"; | $verbose && print STDOUT "$now RECV QUIT command, QUITING\n"; | ||
next; | next; | ||
} elsif ( $line =~ / | } elsif ( $line =~ /SpecialCommand\s{1}?(.*)/ ) { | ||
$now = time; | $now = time; | ||
$verbose && print STDOUT "$now RECV command | $verbose && print STDOUT "$now RECV command Special Command arg: $1\n"; | ||
$verbose && print STDOUT "$now SEND OK: Special Command Result\n"; | |||
$verbose && print STDOUT "$now SEND OK: | |||
$verbose && print $client "OK: Showgraph\n"; | $verbose && print $client "OK: Showgraph\n"; | ||
} else { | } else { |
Latest revision as of 20:06, 3 June 2010
Client
#!/usr/bin/perl -w use strict; use IO::Socket; # initialize host and port my $port = 7890; my $server = "1.2.3.3"; my $verbose = 1; my $sock = new IO::Socket::INET ( PeerAddr => $server, PeerPort => $port, Proto => 'tcp'); die "Could not create socket: $!\n" unless $sock; $now = time; $verbose && print "$now Socket created\n"; my $Now = 0; my $Tower = 56; my $ServerNum = 3; my $Range = 0; my $line; eval { $line = <$sock>; }; if ( $@) { die "$now Failed to read first line\n"; } $now = time; if ( ! $line =~ /HELO/ ) { die "$now Server not ready\n"; } else { $now = time; print "$now got HELO\n" } print $sock "ShowGraph $Now:$Tower:$ServerNum:$Range:ScanQueue:Scan Queue Length:average\n"; $now = time; $verbose && print "$now Sent Show graph\n"; eval { $line = <$sock>; }; if ( $@) { die "$now Failed to get response to Showgreaph\n" } $now = time; if ( $line =~ /OK/ ) { print "$now Got OK: " .$line; } else { print "$now Error: $line \n"; } $now = time; print "$now Sending quit\n"; print $sock "QUIT\n"; close($sock) or die "close: $!";
Server
#!/usr/bin/perl -w use strict; use IO::Socket; my $port = 7890; my $proto = getprotobyname('tcp'); my $now ; my $verbose =1; $|++; my $server = new IO::Socket::INET ( LocalHost => '1.2.3.4', LocalPort => $port, Proto => $proto, Listen => 10, Reuse => 1, ); die "Could not create server socket: $!\n" unless $server; sub myexit { my $sig = shift; print STDOUT time . " $$ Caught $sig on server side\n" ; # close($server); }; sub Wait { print STDOUT time . " Wait called (SIG CHLD signal)\n"; wait; #wait needed to keep <defunct> pids from building up } $SIG{CHLD} = \&Wait; $SIG{QUIT} = \&myexit; $SIG{INT} = \&myexit; $verbose && print time . " Server Socket created\n"; my $client; while ( $client = $server->accept()) { $verbose && print time ." Accepted socket\n"; next if my $pid = fork; die "fork - $!\n" unless defined $pid; $verbose && print time . " Forked!\n"; $verbose && print STDOUT time . " Child Closing Server\n"; # close ( $server ); select $client; $now = time; $verbose && print "$now child selected\n"; while(my $line = <$client>) { chomp $line; if ( $line =~ /QUIT/ ) { $now = time; $verbose && print STDOUT "$now RECV QUIT command, QUITING\n"; next; } elsif ( $line =~ /SpecialCommand\s{1}?(.*)/ ) { $now = time; $verbose && print STDOUT "$now RECV command Special Command arg: $1\n"; $verbose && print STDOUT "$now SEND OK: Special Command Result\n"; $verbose && print $client "OK: Showgraph\n"; } else { $verbose && print STDOUT "$now RECV Unrecognized command: $line\n"; $verbose && print $client "ERROR: Unrecognized command\n"; } } $verbose && print STDOUT time . " child closing client\n"; close ($client); exit(0); } continue { $verbose && print STDOUT time . " Parent Continue, closing client\n"; close($client); $verbose && print STDOUT time . " Parent client closed, sending CHLD to children\n"; kill CHLD => -$$; }