genserv : A Generic Network Server Program
by Chad Clark
Supose you have a program written in a language where you don't want to
learn how to deal with network sockets or maybe a language that doesn't
support sockets directly such as Bash. What if you want to make this
program available on a network? Well you could write a web based front
end. I chose to write genserv (from Generic or General Server).
This works because of the way Linux handles network connections as files.
- There is a C program that listens for a network connect.
- When a connection is made a child process is forked to deal with
the new connection.
- The child process sets the standard file descriptors (STDIN, STDOUT
and STDERR) to all point to the network file descriptor.
- The child then executes the application we want to connect to the
- The application runs and deals with all I/O on the three standard
file descriptors as normal.
The application doesn't need to even know the network connection exists.
The code is all in one C file.
There is also a makefile but it's really simple
and is only used because I like make.
Just put both files in one directory and type make.
From the program's usage description:
./genserv port_num program_name args
"port_num" is a valid TCP port number.
"program_name" is a program to be run when a client connects.
"args" is an optional list of arguments to the program to be executed.
eg: ./genserv 12345 /usr/bin/uptime
will listen on port 12345 and print the uptime string then exit.
eg: ./genserv 12345 /usr/bin/uptime -V
will listen on port 12345 and print the uptime version then exit.
In the future I want to add some sort of basic authentication scheme.
Most likely just a password that is set when genserv is started.
Currently there is no authentication build into genserv so any programs
which get run from genserv need to either do their own authentications
or be open to the network.
It may also be usefull/interesting to read the
for Linux and Unix HOWTO
if you have to write programs which other people will be running.
All of my work was done a stock
Slackware Linux 8.1 machine.
To test the program run ./genserv 12321 ls -l .
From another TTY telnet to port 12321 (eg telnet 127.0.0.1 12321
You should see something like:
[frink@thepurplebuffalo ~/code/cxx/genserv]$telnet 127.0.0.1 12321
Connected to 127.0.0.1.
Escape character is '^]'.
drwx------ 2 frink users 4096 May 17 18:00 CVS
-rw-r--r-- 1 frink users 94 May 17 21:46 Makefile
-rwx------ 1 frink users 16693 May 17 21:46 genserv
-rw-r--r-- 1 frink users 3887 May 17 21:46 genserv.c
-rw------- 1 frink users 0 May 17 21:47 typescript
Connection closed by foreign host.