How to send a file using netcat and then keep the connection alive?
Asked Answered
C

5

9

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)

Concord answered 4/9, 2012 at 16:43 Comment(0)
D
18

Perhaps you were doing:

cat MY_FILE - | ncat ...

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

Daron answered 4/9, 2012 at 21:59 Comment(2)
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 "-"?)Concord
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.Daron
W
8

Server side:

nc -k -l 10000 < my_in_file

Client side:

echo "bye" | netcat 192.168.1.6 10000 > my_in_file -
Whereupon answered 5/9, 2012 at 0:8 Comment(1)
Thank you. However this did not work since server side is not using nc, what william proposed did.Concord
F
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
Forrer answered 12/6, 2014 at 7:1 Comment(0)
C
5

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.

Corded answered 10/12, 2014 at 20:52 Comment(0)
F
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

Fudge answered 19/12, 2015 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.