Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Interesting thing with Netcat (nc) in CentOS 5 or 6 (nc-1.84-10.fc6 or nc-1.84-22.el6.x86_64). How can I work it around? I can't find a bug about this, but

It models a file transfer to a remote host, where the file is transformed, then the result is sent back. In this example transformation is "cat".

The server (localhost):

$ mkfifo nctest.fifo
$ while :; do
      nc -l 5000 <nctest.fifo | cat >nctest.fifo
      echo -n .
      sleep 1
  done

The client (localhost):

$ cat testfile | nc 127.0.0.1 5000 > outfile
$ ls -l
total 9724
prw-r--r-- 1 root root       0 Nov  4 14:17 nctest.fifo
-rw-r--r-- 1 root root 4930560 Nov  4 14:17 outfile
-rw-r--r-- 1 root root 5000000 Nov  4 14:09 testfile

Outfile's size varies from zero to 5000000. I can add "-w2" to client, but it's no use.

share|improve this question

1 Answer 1

Hmmm.... ok, I think I know what's going on here. Since you're starting your server with a pipeline command (using the '|' symbol), there's a relationship between the nc process and the cat process. When the nc processing sees an EOF on the TCP connection, it exits... which then causes the cat process to exit as well (even though it has yet to read everything from the pipe).

You cat fix this by changing your pipeline to something like so:

  nc -l 5000 <nctest.fifo | cat >nctest.fifo &
  wait
  echo -n .

...note, the '&' and wait calls. Of course, this will probably leave a background job running so you'll want to add a SIGCHLD handler to your shell script to make sure you shoot it in the head when you're done (I'll leave that part as an exercise for the ready).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.