# Why does reading from two connected pty's cause an infinite loop?

I want to fake a gsm modem for testing a program. I want the program to send AT-codes to me and that I can answer it back, kind of a VirtualSerialPort. But for some reason the data written from the program is written back to it directly.

I would have expected that socat created two pipes; one that sends the output from the test program to whatever I've attached on the other side and another pipe that works in the other direction. But the data appears to "loop in one pipe", if you understand what I mean. Here's a picture from TTY demystified to illustrate my point:

A simplified reproduction script:

I create the pty's with:

(xterm 1)$socat -d -d -d PTY,link=foo PTY,link=bar 2012/01/18 14:45:01 starting loop with FDs [3,3] and [5,5]  And I listen to both sides: (xterm 2)$ cat <bar
(xterm 3)$cat <foo  If I write a few characters... (xterm 4)$ echo "..." > foo


.. socat logs a lot of traffic that goes forth and back infinitely.

(xterm 1)
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 3 to 5
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 3 to 5
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3


There's nothing outputted from either of the cat commands.

EDIT: @wnoise explained that socat creates two PTY masters. Here's an image to illustrate that:

• What are foo and bar? – psusi Jan 18 '12 at 16:49
• @psusi foo and bar are symlinks to /dev/pts/{number} files. The socat syntax is a bit obscure. – Daniel Näslund Jan 18 '12 at 16:56
• Shouldn't socat be using the master side of the pty? And are you using the same pty that your xterms are using? – psusi Jan 18 '12 at 20:28
• @psusi: I've added an image that illustrates how the master sides of two PTY's get's connected by socat. The pty used by my xterm is not the same as the ones created by socat - I can verify that using the tty command. – Daniel Näslund Jan 19 '12 at 7:07
• +1 for link to TTY Demystified, which is...just awesome. – Alois Mahdal May 20 '12 at 10:57

The syntax you've used creates two pty masters and connects them together bidirectionally. You're getting constant echos because you didn't add "echo=0" to the options. It's probably also necessary to add raw for your use case.