Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socat with a virtual tty link and fork removes my pty link

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.

like image 346
srclosson Avatar asked Dec 16 '22 03:12

srclosson


2 Answers

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.

like image 59
srclosson Avatar answered Dec 31 '22 13:12

srclosson


why not just use "unlink-close=0"?

e.g.

socat -d -d PTY,link=/dev/ttyV1,waitslave,echo=0,raw,unlink-close=0 TCP-LISTEN:11313,reuseaddr,fork
like image 24
haceaton Avatar answered Dec 31 '22 12:12

haceaton