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

时光总嘲笑我的痴心妄想 提交于 2019-12-04 20:54:44

问题


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)


回答1:


Perhaps you were doing:

cat MY_FILE - | ncat ...

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




回答2:


Server side:

nc -k -l 10000 < my_in_file

Client side:

echo "bye" | netcat 192.168.1.6 10000 > my_in_file -



回答3:


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



回答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.




回答5:


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

  • 'con' means console input, see http://ss64.com/nt/type.html
  • It works with binary files.


来源:https://stackoverflow.com/questions/12267905/how-to-send-a-file-using-netcat-and-then-keep-the-connection-alive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!