Sockets – socat with a virtual tty link and fork removes the pty link

listenerserial-portsocatsocketstcp

I require to be able to multiplex tcp connections to a single virtual tty for testing software that communicates over a serial port. I have been using the following command to create a server that listens on a tcp port, and then forwards data to the PTY.

socat -d -d PTY,link=/dev/ttyV1,waitslave,echo=0,raw TCP-LISTEN:11313,reuseaddr,fork

My problem is when a tcp client on port 11313 disconnects, socat removes my link to /dev/ttyV1, thus killing the session to everyone else who is connected.

Is there a way to retain this link? Or perhaps create a pseudo tty outside of socat so that I don't have to worry about maintaining it? I've tried /dev/tty1 and /dev/ttyS1 and I've not had any luck. My command for these was:

socat -d -d file:/dev/tty1,nonblock,echo=0,raw TCP-LISTEN:11313,reuseaddr,fork

My requirement: Be able to connect a piece of software to a serial tty on one end, and then connect a number of tcp clients on the other. Tcp clients messaging is properly serialized and multiplexed so that messages don't arrive on top of each other.

Best Solution

Okay, I seem to have found a solution for myself. You have to create a virtual null modem pair first, then open one end of the virtual null modem pair and link it with the TCP listener.

socat -d -d PTY,raw,echo=0,link=/dev/ttyVA00 PTY,raw,echo=0,link=/dev/ttyVB00

Then

socat -d -d open:/dev/ttyVA00,nonblock,echo=0,raw TCP-LISTEN:11313,reuseaddr,fork 

I can connect as many clients as I want, and everything seems to be working.

Related Question