Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 # 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/

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

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 # 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 # 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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2022-09-24 18:39 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (115 votes). Check out past polls.