Janus Troelsen
Janus Troelsen

Reputation: 21298

How to remove EOF's from pipes? Or: How to use tail -f with netcat?

I am trying to pipe XBoard chess commands over TCP. I understand that nc will close a connection when it sees EOF.

Example 1

$ nc -l 1301 | hd &
[1] 10241
$ echo -en "babab" | nc localhost 1301
00000000  62 61 62 61 62                                    |babab|
00000005
[1]+  Done                    nc -l 1301 | hd
$

That is my problem and I just think I need to find out how to make it so that the nc -l command above don't terminate. I have read that I could use tail -f, but that doesn't seem to work unless I use files or FIFO's. Now here is an explanation of a similar problem (I think) that caused me to search for a solution to this problem:

Example 2

I want to run an XBoard Chess engine in the browser and communicate over WebSockets. I therefore launch XBoard like this:

./websockify 2023 -- xboard -fcp "nc -q -1 -k -l 2023"

It starts up and websockify seems to buffer the initial commands from XBoard. I now connect the browser like this:

ws = new WebSocket("ws://localhost:2023/", "base64");
ws.onclose = function(){console.log("close");};
ws.onmessage = function(evt){console.log(window.atob(evt.data));};
ws.onopen = function(){console.log("open");}

(executed in the console on one line)

It connects and I make the first move in XBoard as white, this is the output in the browser console:

open
xboard
protover 2
[2 second delay]
[other commands]
time 30000
otim 30000
b2b3

Everything is good. Now I make a move as black, from the browser: ws.send(window.btoa("move b7b5\n")); Works too.

Now, when I go and make the third move of the game in XBoard, it doesn't work anymore. Immediately after mouseup this console output appears:

  1: 127.0.0.1: Target closed
xboard: Error writing to first chess program: Broken pipe
xboard: Error writing to first chess program: Broken pipe
xboard: Error writing to first chess program: Broken pipe
xboard: Error: first chess program (nc -q -1 -k -l 2023) exited unexpectedly

The GUI shows the same.

So my hypothesis is that an EOF is somehow sent from XBoard to netcat after the first move. This doesn't really make sense, cause how come didn't websockify report "target closed" earlier? And what made the first move so different from all the other commands XBoard sent?

Upvotes: 3

Views: 1747

Answers (1)

Janus Troelsen
Janus Troelsen

Reputation: 21298

Yes! I found a solution.

I changed the xboard "engine" command to ./runserver.sh and wrote runserver.sh as (execute flag set):

#!/bin/sh
nc -q -1 -k -l 2023 | tee /dev/null

Everything works now!

Upvotes: 3

Related Questions