How to Close a Netcat Connection After a Certain Character Is Returned in the Response

How do you close netcat connection after receipt of data?

I think including the -c flag for nc should do it. Also, are you sure you want to use Bourne shell, and not Bash? I'd suggest changing your shebang to #!/bin/bash

Client doesn't close connection to server after receiving all responses

Finally found a working way (maybe not the best but at least it's perfectly doing what I want :D)

after all functions I send a "ENDRESPONSE" message, and on my client, I test if I have this message or not :

function sendMessage {
while read line; do
if [[ $line == "ENDRESPONSE" ]]; then
break
else
echo $line
fi
done < <(netcat "$ipAddress" "$port" <<< "$*")
}

Anyway thanks for your help, I'll try to implement other solutions later !

Netcat only sends answer after I CTRL-C the connection

man nc writes:

"netcat stays running until the network side closes" so your browser will wait forever for the data to finish. Use -q option to quit, see the man page again: "after EOF on stdin, wait the specified number of seconds and then quit".

cat index.html | nc -lnvp 2222 -q 0

nc -l 2222 does not make sense, you need to use -p to specify the port.

Passing parameter with special characters to Socat exec script

Socats address parser interpretes a few characters specially, these are for example:

: , ! " ' \ ( [ {

You have to escape them with backslash. Please note that you also need to escape a few special characters including \ from the shell parser.

Use options -d -d -d -d to see the parameters socat receives and uses; try something like

socat -u /tmp/nonexistingdir/x\\\\x -

to see in the error message what is left of the special characters when socat performs the final system call.

Send String over netcat connection

Try this:

echo -n 'Line of text' | nc <ip> <port>

You can also use temp file syntax:

cat <(echo "Line of test") | nc <ip> <port>


Related Topics



Leave a reply



Submit