8

I am issuing the command:

netcat serveraddress myport < MY_FILE

The thing is, that netcat sends a message to close the connection once the file is sent. I need to write messages from the console after sending that file. I remember to have done something to pipileline to stdin.. was it this?

netcat serveraddress myport < MY_FILE | 

That isn't working now.

I'm on unix.

Extra info: This did not assume control on server side (E.G. use netcat on serverside to listen for the inbound connection)

quinestor
  • 1,432
  • 4
  • 19
  • 40

5 Answers5

18

Perhaps you were doing:

cat MY_FILE - | ncat ...

(Note that I've intentionally mispelled netcat, because I believe ncat is a superior program.)

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Thank you, that was it. A big plus would be if you can add a little info about why that works, or some refference. (What's that "-"?) – quinestor Sep 05 '12 at 13:22
  • 3
    The `-` tells cat to read stdin. It first reads (and writes) `MY_FILE`, then reads stdin. If stdin is a tty, then it simply blocks on input from the keyboard. – William Pursell Sep 05 '12 at 13:26
8

Server side:

nc -k -l 10000 < my_in_file

Client side:

echo "bye" | netcat 192.168.1.6 10000 > my_in_file -
TOC
  • 4,326
  • 18
  • 21
6

To keep listening for other connections use -k on nc.

suppose you want to make connection to server , server write to file and print to stdout ?

Server:

nc -k -l $PORT | tee file ( or > file without print to stdout)

Client

nc $IP $PORT < file_to_send
Ravexina
  • 2,406
  • 2
  • 25
  • 41
4

You can use the -q -1 option of nc:

echo MY_FILE | nc -q -1 192.168.0.1 9000

This way it will also work if the command is run in background.

1

I realise this thread is very old but and the OP uses unix, for reference here is a windows equivalent to "cat FILE - | ncat HOST":

type FILE con | ncat HOST

Then type CTRL-Z or CTRL-C to end the connection.

Notes

timyhac
  • 373
  • 3
  • 9