Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socat terminates after connection close

Tags:

linux

socat

This comand (serial port redirector) accepts a single connection on TCP:11313 :

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

However when the connection is lost, the above socat process is killed and the client is not able to connect.

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

Any ideas how to achieve this?

like image 422
UpCat Avatar asked Jan 19 '15 19:01

UpCat


People also ask

What does Socat do in Linux?

Socat allows for bidirectional data transfers from one location to another. The socat utility is a relay for bidirectional data transfers between two independent data channels.

What does fork do in Socat?

The fork option just makes it fork a new child to process the newly accepted connection while the parent goes back to waiting for new connections.

What is the Socat command?

Socat is a flexible, multi-purpose relay tool. Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.

Can you connect to Socat with netcat?

Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party. It can act like a simple ad-hoc chat program. Socat can talk to Netcat and Netcat can talk to Socat.


1 Answers

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

LISTEN option group, options specific to listening sockets

max-children= Limits the number of concurrent child processes [int]. Default is no limit.

With this you can limit the number of clients that can interact with the PTY to one, but won't prevent others from connecting. Others will simply queue until the first connection is closed. If you want to prevent that, I'd suggest to just wrap the socat call in a while true; do ..; done loop:

while true; do
  socat PTY,link=/dev/ttyV1,echo=0,raw,unlink-close=0 TCP-LISTEN:11313,forever,reuseaddr
done
like image 170
Phillip Avatar answered Sep 28 '22 06:09

Phillip