Socat terminates after closing the connection

This command (serial port redirector) accepts one TCP connection: 11313:

socat PTY,link=/dev/ttyV1,echo=0,raw,unlink-close=0 TCP-LISTEN:11313,forever,reuseaddr 

However, when the connection is lost, the aforementioned socat process is killed and the client cannot connect.

I can solve this problem by adding the fork option at the end of the above command. But then several clients will be able to connect. But I want to accept only one connection.

Any ideas how to achieve this?

+5
source share
1 answer

You can limit the number of children using the max-children option:

LISTEN parameter group, parameters specific to listening sockets

Max Children = Limits the number of parallel child processes [int]. There are no restrictions by default.

With this, you can limit the number of clients that can interact with PTY, but will not prevent others from connecting. The rest will simply queue until the first connection is closed. If you want to prevent this, I would suggest just wrapping the socat call in the while true; do ..; done while true; do ..; done while true; do ..; done :

 while true; do socat PTY,link=/dev/ttyV1,echo=0,raw,unlink-close=0 TCP-LISTEN:11313,forever,reuseaddr done 
+7
source

Source: https://habr.com/ru/post/1211443/


All Articles