I'm running some performance testing, and I'm trying to send the same file repeatedly to a socket.

If I do something like:

$ socat -b1048576 -u OPEN:/dev/zero TCP4-LISTEN:9899,reuseaddr,fork 
$ socat -b1048576 -u TCP: OPEN:/dev/null

Then with that 1MB buffer iftop tells me that I'm pushing 20Gbps.

However, what I'm really trying to do is something more like:

$ socat -b1048576 -u OPEN:somefile.dat TCP4-LISTEN:9899,reuseaddr,fork 
$ myprog TCP: > /dev/null

But it only pushes that somefile.dat one time, I'd really like it to rewind() to the beginning and send it again.


Assuming that you want to open and send the same file at each new connection, you can use -U, the reverse direction to -u, and the reversed addresses, as in the following

socat -b1048576 -U TCP4-LISTEN:9899,reuseaddr,fork OPEN:somefile.dat
socat TCP: - >/dev/null

If you want the file to repeat ad infinitum, you can use something like this:

socat -b1048576 -U TCP4-LISTEN:9899,reuseaddr,fork SYSTEM:'while cat somefile.dat;do \: ;done'
  • That's pretty close to what I wanted, but it does force the client to reconnect each time. – Alun Apr 22 '16 at 3:47
  • @Alun if you want an infinite file, like /dev/zero you can add a loop, see my updated answer. – meuh Apr 22 '16 at 6:12
  • Thanks. I think that's the best that can be done. Was really hoping for something that would be functionally equivalent to while(1) sendfile(soc, file, 0, size); for maximum performance... – Alun Apr 27 '16 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.