Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

netcat and re-stablishing network connection 1

Status
Not open for further replies.

geebers

Technical User
May 29, 2009
20
US
Consider the following netcat command to listen on a port.....

sudo nc -l -u -p 3333 | myscript.sh

contents of myscript.sh is....
#!/bin/sh
while read INPUT; do
echo "Input is $INPUT | tee /tmp/log.$$
done

This waits for a connection on port 3333 and when a connection is made, data sent by the client is piped to myscript.sh for processing. Fine.

So if I use the following command to make a connection to port 3333.....

sudo nc -u 192.168.1.100 3333

and I type some text and hit enter, myscript.sh recieves the string of text. Fine.

But if I kill the client connection by typing Ctrl-C, then the client exits, and if I try to re-establish another client connection again with the same nc command, the server does not establish a new connection. How can my listening "server" re-establish a new connection? I've read a little about the trap command, and thinking that it belongs somewhere to handle interrupts. But where? Or something else to help re-establish another nc command to listen on port 3333?

Thanks for any help.
 
Hi

geebers said:
I've read a little about the trap command
You mean the [tt]trap[/tt] shell built-in ? That is for handling signals. Not a big help here.
geebers said:
Or something else to help re-establish another nc command to listen on port 3333?
Correct. You have to run [tt]netcat[/tt] in a loop :
Code:
while :; do
  sudo nc -l -u -p 3333 | myscript.sh
done
Note that simultaneous request will still not be handled.


Feherke.
 
ok,
so I've created a new script called listen3333.sh and contains.....
#!/bin/sh
while :; do
nc -l -u -p 3333 | ./myscript.sh
done

and then make it executible...
chmod 755 listen3333.sh

myscript.sh contains....
#!/bin/sh
while read INPUT; do
echo "Input is $INPUT" | tee >>/tmp/log.$$
done

now run listen3333.sh....
./listen3333.sh
now server is listening on port 3333

now, echo some text to port 3333...
nc -u 192.168.1.100 3333
"type some text and hit enter"

Hooray, the text is echoed and appended to /tmp/log.$$

now type Ctrl-C to kill the client connection.

now re-establish a new client connection...
nc -u 192.168.1.100 3333
"type some text and hit enter"

now nothing is being echoed and nothing is appended unless i manually restart listen3333.sh

i'm so very close.
any clues?
 
O.k. thanks. Yes, seems nc still runs. So, I've updated my listen3333.sh script to the following and it is behaving like I want it to.....

#!/bin/sh
while :; do
nc -l -u -p 3333 | while read INPUT; do
date >>/tmp/log.txt
echo "$INPUT" >>/tmp/log.txt
killall nc
done
done

server script starts a socket connection on port 3333 and listens, reads whatever text coming in on port 3333 and appends it to /tmp/log.txt, then kills nc process, then starts all over again opening a new socket connection and listens. I only do this in case of a network problem while a client connects. I'm using wi-fi in a large area and it experiences network "breaks" from time to time. this ensures that the client can re-connect. I'm sure there are better ways, but for what I'm doing, its acceptable. Thanks again.
 
Not sure if it's useful in this context, but nc has a -w option which specifies an inactivity timeout period after which it will disconnect and die. By default it is 0 = never. It may save you having to kill it each time...

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top