How to use nc to listen on a TCP port indefinitely?

0

I want to read the data arriving on a specific TCP port indefinitely and dump the data into a file. I have tried many different variations of nc but it is not working out for me. For the most part I am not getting any error on the console. Below is what I have tried:

nc -lv 12345 >> ../myfile
nc -v 12345 >> ../myfile
nc -v -p 12345 >> ../myfile
nc -v localhost -p 12345 >> ../myfile

Appreciate your help..

user422171

Posted 2018-06-06T15:36:05.037

Reputation: 13

What was your intention with using -v and -p? – Attie – 2018-06-06T15:53:55.403

@Attie -v to see what it is receiving and -p for port number. – user422171 – 2018-06-06T16:13:55.513

Yes, but why would that be useful to try and make it keep listening? – Attie – 2018-06-06T16:15:44.193

@Attie It was just for testing the command if it works and to know how it works. Was gonna remove it eventually.. – user422171 – 2018-06-06T16:39:15.153

Answers

0

I want to read the data arriving on a specific TCP port indefinitely

This doesn't really make sense... if you were after all data arriving on a UDP port, then fine - TCP however is a connection-based protocol. Once a client connects, and you accept(), you end up with two sockets - one listening, and one connected to the client... nc will close the original listening socket, and deal one-on-one with the client.


If you'd like to be able to listen for multiple clients connecting, then try using socat:

socat TCP-LISTEN:12345,fork FD:1 >> ../myfile

This will setup a listening socket and fork a new process on connection - keeping the listening socket listening. All data received will be written to stdout (file descriptor 1), which is redirected to ../myfile for appending.

NOTE: data will be received from any number of clients, in no guaranteed order with no framing... i.e: it'll be a mess of jumbled data if you have more than one client at a time.


If you want socat to handle the file for you, then you can use one of CREATE or OPEN (see the man page):

socat TCP-LISTEN:12345,fork OPEN:../myfile,append

If you are content with a "one client at a time" approach, then put nc in a loop:

while :; do nc -l 12345; done >> ../myfile

Test this with nc:

nc localhost 12345

Attie

Posted 2018-06-06T15:36:05.037

Reputation: 14 841

I should have made it clear in the question. I have only one client sending on that port. I just need to grab everything coming on that port and put it in the file – user422171 – 2018-06-06T16:48:02.680

... then what's wrong with nc -l 12345 >> ../myfile? – Attie – 2018-06-06T17:04:27.650

It creates the file but it never gets written. I have checked the client sending a string every 2 seconds. – user422171 – 2018-06-06T17:21:23.377

I guess what you have mentioned in the answer is the cause. I am not creating the socket. I am attempting to subscribe to the port and start listening without a socket connection – user422171 – 2018-06-06T17:22:21.077

0

the screen on the left

nc -l 1234 starts the server.

the screen on the right nc 127.0.0.1 1234 connects.

it looks like you got your commands wrong.

Worth knowing also whether you're using the gnu one or the bsd one, mine is the bsd one. man nc shows "NC(1) BSD General Commands Manual". To start the server on the gnu one it might need -l with -p. To connect the client to the server on whatever port, will never use -p.

enter image description here

You can use -v as well

enter image description here

Your immediate problem was that you weren't getting it running at all.

Your question asks about it being indefinite, and i'd note though that if you do ctrl-c at the client side then besides breaking the connection, it stops the server. You can put that server listening in a loop. That works. So then the server continues listening and is still there if you try to connect again after a ctrl-c from the client.

enter image description here

barlop

Posted 2018-06-06T15:36:05.037

Reputation: 18 677

When I start server I get this: "Warning: Inverse name lookup failed for `0.0.48.56'" – user422171 – 2018-06-06T18:46:37.850

Also the client command does nothing – user422171 – 2018-06-06T18:59:07.177

@user422171 have you tried adding -n e.g. nc -lvn 12345 and $ nc -vn 127.0.0.1 12345 https://stackoverflow.com/questions/48779583/nc-command-inverse-host-lookup-failed-unknown-host And at the client side after it connects, try typing things and pushing ENTER. It should be what I think may be called an echo server i.e. what ever you type(and push enter after), at the client, will get echoed at the server.

– barlop – 2018-06-06T20:38:44.503

Tried nc -lvn 12345 and got Error: Couldn't resolve host "12345" – user422171 – 2018-06-06T22:08:51.803

@user422171 any idea what version of nc debian uses by default, or what version you have? what if you try nc -n -l -p 12345 ? or nc -n -p 12345? – barlop – 2018-06-06T22:35:56.897

Version is netcat (The GNU Netcat) 0.7.1 – user422171 – 2018-06-07T20:16:42.943

@user422171 okay.. mine is BSD .. so I can't really comment much.. I did use the GNU one before and I think it was nc -l -p 12345 or nc -lp 12345 or if you want to try adding -n to get rid of that warning you get about a name lookup issue, then nc -n -l -p 12345 or -nlp 12345. If that works you only have to figure out how to connect the client. e.g. see here it looks like gnu nc and they do both server and client https://i.imgur.com/uVow4SB.png

– barlop – 2018-06-07T22:55:02.917

Thank you! tried nc -nlp 12344 Got error Error: Couldn't setup listening socket (err=-3) I think this is some progress because for the first time in last few days, it says it tried to create a listening socket.. – user422171 – 2018-06-11T14:34:33.397