Fundamentals 6 min read

Creating a Simple TCP Server with inetd/xinetd and Shell Scripts

This article demonstrates how to build a minimal TCP server on Unix by configuring inetd or xinetd to redirect socket traffic to a shell script that reads from standard input and writes to standard output, illustrating core networking and scripting concepts.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Creating a Simple TCP Server with inetd/xinetd and Shell Scripts

The article begins by posing the question of how to quickly implement a TCP socket server or a minimal HTTP server, suggesting the use of Unix shell and scripting tools for a fast solution.

It then analyses the power of shell and script languages in Unix, emphasizing their ability to handle files—including standard input and output—and their superiority in pipeline processing, leading to a design where a script reads requests from stdin and writes responses to stdout, effectively acting as a TCP service.

The implementation section introduces inetd, the classic super‑daemon that can redirect a TCP socket’s data stream to a command’s standard input and output, and briefly mentions xinetd as a modern replacement with slightly different configuration files.

Example – inetd configuration

1. Add a service definition to /etc/services: laser 54321/tcp #laser test 2. Append an entry to /etc/inetd.conf:

# laser test
laser stream tcp nowait root /root/laser.sh a

3. Write a simple bash script ( /root/laser.sh) that reads a line and echoes a greeting:

#! /bin/bash

read A
echo " hello $A!";

4. Make the script executable: chmod +x /root/laser.sh 5. Restart or reload inetd (e.g., sudo killall -HUP inetd or /etc/rc.d/rc.inetd restart).

The article also notes that many Linux distributions now prefer xinetd, though the configuration steps are similar.

Testing

After the service is up, you can test it with telnet:

bash-4.2$ telnet 192.168.126.241 54321
Trying 192.168.126.241...
Connected to 192.168.126.241.
Escape character is '^]'.
laser
!hello laser

The final sections reflect on how a TCP socket appears as a file descriptor in Unix, how shells can manipulate these descriptors, and why understanding this mapping is valuable for assessing a candidate’s foundational Unix knowledge.

A postscript mentions that the example originated from an interview scenario used several years ago and includes a citation note.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Network programminginetdtcp serverxinetd
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.