Netcat Socket Programming

1.1k views Asked by At

I am facing some issues to solve a problem. A server program is giving 4 random unsigned integer numbers everytime I connect to it. My job is to add the 4 numbers and send back the sum. But the time is restricted. So if I want to use a calculator to do the job, it is not possible. Thus I have to take resort to scripting. I want to connect to the the server using netcat, retrieve the 16 bytes, pipe the data to my C program(which will parse the data into 4 unsigned int variables and print out the sum) and again redirect this program output to netcat

netcat <server> <port> | myProg | netcat <server> <port>

But the tcp socket netcat is opening second time is not the previous socket. So a different set of 4 numbers would be presented this time, which defeats the entire effort. My question is: Is it possible by any means(using netcat) to use the previous socket (opened by netcat) to pump the calculated sum back to the server?

I dont want to use C to do socket programming as it is very tough for me. Also I know

echo 3<>/dev/tcp/server/port
cat <&3 | ./myprog | more >&3

probably will solve my purpose (correct me if I am wrong). But I would like to do it the netcat way. Thanks in advance.

1

There are 1 answers

1
Ben Voigt On

Use socat instead of netcat.

socat can spawn the command itself, with input and output both redirected (you don't use shell redirection, let socat handle it)

socat TCP:server,port SYSTEM:myProg