For basic network communication nc
or netcat, as suggested by joan, is sufficient. However, if you for example want to forward a serial console or encrypt the connection socat
is probably more convenient. For inspiration here are two working examples of the mentioned applications.
Serial console via TCP
To connect directly to the serial port with socat the following command can be used:
socat -,raw,echo=0,escape=0x1d /dev/ttyUSB0,b115200,echo=0,raw
The escape char is set to Ctrl-].
This can be expanded into a tcp-server like this:
socat tcp-listen:54321,reuseaddr /dev/ttyUSB0,b115200,raw,echo=0
This will allow only one client to connect, as per the OPs request. Wrap it with a while-loop to keep the server going between reconnects:
while true; do
socat tcp-listen:54321,reuseaddr /dev/ttyUSB0,b115200,raw,echo=0
done
Now you can connect to the serial port through the tcp-server like this:
socat -,raw,echo=0,escape=0x1d tcp:server-host:54321
Serial console via SSL
To encrypt the connection, you first need to generate two key-pairs, see Example for openssl connection using socat for the details, here is a summary:
# Generate self signed server certificate
openssl genrsa -out server.key 1024
openssl req -new -key server.key -x509 -days 3653 -out server.crt
cat server.key server.crt > server.pem
chmod 600 server.key server.pem
# Generate self signed client certificate
openssl genrsa -out client.key 1024
openssl req -new -key client.key -x509 -days 3653 -out client.crt
cat client.key client.crt > client.pem
chmod 600 client.key client.pem
Copy the pem-files to their respective hosts, do the opposite with the crt-files, e.g.:
scp server.pem client.crt server-host:
scp client.pem server.crt client-host:
Now the server can be run like this:
socat openssl-listen:1443,reuseaddr,cert=server.pem,cafile=client.crt /dev/ttyUSB0,b115200,echo=0,raw
Connect to the serial console like this:
socat -,raw,echo=0,escape=0x1d openssl:server-host:1443,cert=client.pem,cafile=server.crt