Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Using inetd to serve a socket

It is often the case that one needs to test a system before the hardware is available or on-line. In my case, I'm developing an interface which will talk to a socket on a complex piece of Fab Metrology gear called an Applied Materials NanoSEM, using a complex protocol called SEMI SECS-II/GEM HSMS. I needed a quick and dirty handler to act as the NanoSEM while I get the protocol parser working.

UNIX-like systems such as FreeBSD and Linux have a nifty feature called inetd, which comes to our rescue. inetd runs a program you specify whenever somebody else tries to connect to the socket you've chosen. By making a few simple configuration improvements, we can send our input to the specified socket, and inetd invokes our program, passing our input to it as STDIN. Our handler then processes it and spits out its response as STDOUT back to our socket. Cool, huh? What's even more cool is that if another process (or system) also tries to connect to the same socket, inetd will invoke another copy of our handler without bothering the first one.

Interested? Okay, here's the code...

The server handler:

#!/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"; }
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:
# chmod +x /usr/local/bin/sinet.pl


The configuration:

Okay, now, here's the setup. In /etc (you need to be superuser), edit /etc/services to add your port number to the known services list, making up a unique name for the service. My port is 6100, its service is 'secshsms', and I've asked it to handle both stream (tcp) and datagram (udp) packets, although this example will only deal with tcp.
secshsms 6100/tcp # DSW Handler for SECS-II/GEM HSMS traffic secshsms 6100/udp # DSW
Next, edit /etc/inetd.conf to attach your program to that service:
# DSW add-on for SECS-II over HSMS secshsms stream tcp nowait nobody /usr/local/bin/sinet. +pl secshsms
Okay, find inetd and restart it.
# ps -ax | grep inetd 563 ?? Is 0:00.01 /usr/sbin/inetd -wW -C 60 # kill -1 563
From now on, any process that attempts to talk to my machine's port 6100 gets its output routed to my handler sinet.pl.

With that in hand, here's a sample client, adapted from Perl Cookbook recipe 17.10. It can be installed on any machine within a routable network (i.e., no firewall) and it will talk to my handler.

A simple client:

#!/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;
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.

UPDATE 1: changed server user to nobody, thank you idsfa.

Don Wilde
"There's more than one level to any answer."

In reply to A Simple Socket Server Using 'inetd' by samizdat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (12)
    As of 2014-09-23 16:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (232 votes), past polls