This little program will process anything that comes in on the specified port, clearing carriage returns and line feeds, and (in this simple case) spitting it back with two spaces in front and a newline at the end. An input containing "endit" causes the handler to exit, and, thanks to the flush sequence at the top, output is immediate. Make your program executable:#!/usr/bin/perl -w -T # sinet.pl A simple inetd socket server. use strict; my $old_fh = select(STDOUT); $| = 1; select($old_fh); while( my $line = <STDIN> ) { $line =~ s/\r?\n$//; if ($line =~ /endit/) { die "shutting down\n"; } # do your processing here! print " $line\n"; }
# chmod +x /usr/local/bin/sinet.pl
Next, edit /etc/inetd.conf to attach your program to that service:secshsms 6100/tcp # DSW Handler for SECS-II/GEM HSMS traffic secshsms 6100/udp # DSW
Okay, find inetd and restart it.# DSW add-on for SECS-II over HSMS secshsms stream tcp nowait nobody /usr/local/bin/sinet. +pl secshsms
From now on, any process that attempts to talk to my machine's port 6100 gets its output routed to my handler sinet.pl.# ps -ax | grep inetd 563 ?? Is 0:00.01 /usr/sbin/inetd -wW -C 60 # kill -1 563
Working from this skeleton, a more elaborate language can be developed. The program on each end can be made to parse and respond to commands from the other.#!/usr/bin/perl # ncliauto.pl a simple automated client use warnings; use strict; use IO::Socket; my ( $confstr, $host, $port, $kidpid, $handle, $line, @say ); # the config file contains host name (or IP addr) and port number, wit +h a space between # examples: localhost 6100 # myserv.mynet.com 6100 # 123.456.789.1 6100 open( CONF, "<./hsms.conf") or die "conf file: $!\n"; $confstr = <CONF>; close( CONF ) or die "closing conf file: $!\n"; chomp $confstr; ( $host, $port ) = split( /\s+/, $confstr ); # This is our demo array of outputs sent to the handler @say = ( 'You are getting sleepy...', '... very sleepy.', 'Your eyes are getting very heavy!', "... it's so hard to hold them open.", "You're so very sleepy now.", 'You just want to go to sleep.', 'Sleep feels so good!', "You're asleep. Sleep!", "You've earned it, just relax and sleep!", "... Sleep!", " Sleep!", " Sleep!", '', '... zzz... zzz... ...zzz ...', 'endit'); # This creates our client socket $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) or die "can't connect to port '$port' on host '$host': $!\n"; # make sure it turns around inputs immediately $handle->autoflush(1); # announce our connection print STDERR "[connected to $host:$port]\n"; # fork a child to handle sending our data to the socket die "can't fork: $!\n" unless defined($kidpid = fork()); if ( $kidpid ) { # The parent handles data coming from the socket server to us while ( defined( $line = <$handle> ) ) { print STDOUT $line; } # ... until the connection is broken kill( "TERM" => $kidpid ); } else { # The child process receives data for us foreach my $item ( @say ) { print $handle $item . "\r\n"; sleep 1; } } exit;
In reply to A Simple Socket Server Using 'inetd' by samizdat
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |