How do I send and receive simultaneously with netcat?

1

1

I need to test a TCP server by connecting with netcat as a client sending a stream from /dev/random and at the same time pipe the output to /dev/null.

Receiving:

nc 127.0.0.1 3000 > /dev/null

Sending:

cat /dev/random | nc 127.0.0.1 3000

How can I do both at same time?

I can only connect one client and I need to pipe output to /dev/null for fast consumption of the output (~50MB/s).

JeffV

Posted 2012-07-18T17:03:38.870

Reputation: 141

Answers

1

Use nc 127.0.0.1 3000 >/dev/null & to send that command to background. Then you can run the other command. Check the jobs commands as well as fg and bg to see how to switch foreground and background jobs.

ott--

Posted 2012-07-18T17:03:38.870

Reputation: 1 911

No, this would not work. I need to send to stdin of netcat and pipe stdout of same netcat process to /dev/null. – JeffV – 2012-07-19T12:20:05.693

netcat can open only 1 network connection. For a client/server model, you need 2 processes. See the example on the manpage at http://linux.die.net/man/1/nc – ott-- – 2012-07-19T13:13:55.497

Not looking for more than one connection. In this case server by design is restricted to only one client at a time. Since, the TCP connection is bidirectional, and I am trying to send random bytes to the server while consuming everything it sends to the client as fast as possible. – JeffV – 2012-07-19T19:00:32.167

Ah, you're looking for the echo service. Check your /etc/inetd.conf or xinetd.conf, echo is usually disabled. chargen is also a nice thing to play with. – ott-- – 2012-07-19T19:56:16.363

0

Apparently, this works:

cat /dev/random | netcat 127.0.0.1 3000 > /dev/null

JeffV

Posted 2012-07-18T17:03:38.870

Reputation: 141

>/dev/null is much shorter. – ott-- – 2012-07-19T19:49:36.930