6

inetd can make several programs with stdin input and stdout output work like programs with input and output from and to sockets, and monitor their listening sockets simultaneously.

Is there a simpler program than inetd which just works for a single program: make a single program with stdin input and stdout output work like a program with input and output from and to sockets?

Thanks.

Tim
  • 98,580
  • 191
  • 570
  • 977
  • 3
    If your Linux distribution is using systemd, then systemd can do that all for you (in other words, you don't need any extra software), simply by configuring a [socket unit](https://www.freedesktop.org/software/systemd/man/systemd.socket.html) and a corresponding service unit to run your program. – filbranden Feb 14 '19 at 18:17

3 Answers3

16

Nmap’s Ncat can do this, with its -c or -e options:

nc -l -c bc

will listen on the default port (31337) and, when a connection is established, run bc with its standard input and output connected to the socket.

nc localhost 31337

will then connect to a “remote” bc and you can then enter bc expressions and see their result.

socat can do this too (thanks Hermann):

socat tcp-listen:31337,reuseaddr,fork EXEC:bc
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
11

There are plenty of UCSPI-TCP tools.

In the following, the server program is ./service, 0.0.0.0 or ::0 are the host IP addresses, and 7777 is the port number.

There are also (not listed in this answer but documented in the various tool collections, q.v.) UCSPI-UNIX and UCSPI-LOCAL tools for AF_LOCAL sockets, tools for doing the same with FIFOs, UCSPI-SSL tools for TLS/TCP sockets, and tools for Netlink sockets.

Bernstein ucspi-tcp

In Daniel J. Bernstein's ucspi-tcp, there is tcpserver:

tcpserver -v -P -R -H -l 0 0.0.0.0 7777 \
./service

There are IPv6-capable enhanced versions of Bernstein ucspi-tcp such as Erwin Hoffman's tcpserver:

tcpserver -v -P -R -H -l 0 ::0 7777 \
./service

Bercot s6-networking

Laurent Bercot's s6-networking has s6-tcpserver4:

s6-tcpserver4 -v 0.0.0.0 7777 \
./service
and s6-tcpserver6:
s6-tcpserver6 -v ::0 7777 \
./service
These are shims for other s6-networking tools.

nosh UCSPI tools

The nosh toolset has tcp-socket-listen and tcp-socket-accept:

tcp-socket-listen --combine4and6 :: 7777 \
tcp-socket-accept --verbose --localname 0 \
./service
It also has a tcpserver that is just a shim for the other two and that defaults several options on:
tcpserver -v -l 0 :: 7777 \
./service

Pape ipsvd

Gerrit Pape's ipsvd has tcpsvd:

tcpsvd -v 0.0.0.0 7777 \
./service

Sampson onenetd

Adam Sampson has a onenetd:

onenetd -v :: 7777 \
./service

Further reading

JdeBP
  • 66,967
  • 12
  • 159
  • 343
  • 4
    This answer would be improved by leading in with the connection between the question (loosely, "programs talking to sockets") and the terminology used in the answer ("UCSPI"). If you explain what UCSPI is, then everything else will be clearer. Like most things djb, it's a boutique term, and not widely recognized. – gowenfawr Feb 14 '19 at 21:31
1

In addition to the programs listed by JdeBP, courier also uses a wrapper like this, called couriertcpd.

Ángel
  • 3,383
  • 1
  • 13
  • 16