2

Scenario

Whenever the netcat server receives a connection, I want it to sleep for 2s before returning a HTTP response.

I know we can turn netcat into a simple HTTP server by something like nc -lp 3000 < httprespose.

Question

How do I simulate the 2s delay?

Tran Triet
  • 675
  • 1
  • 11
  • 27
  • is socat a completely different binary from netcat? – Tran Triet Apr 09 '20 at 05:39
  • 1
    I don't know how you define "completely different binary". I know `socat` and `netcat` are different tools. – Kamil Maciorowski Apr 09 '20 at 05:46
  • Thanks. Just a side question - I was trying to get good at `netcat` since it's so versatile and recommended by many people. Are there anything `netcat` can do but `socat` can't in your opinion? – Tran Triet Apr 09 '20 at 05:49
  • 1
    I know there are things `netcat` cannot do but `socat` can. I *suspect* all things `netcat` can do, `socat` can do as well. Note there are at least two major implementations of `netcat` (aka `nc`). There is also `ncat`. See [this](https://unix.stackexchange.com/q/368155/108618). – Kamil Maciorowski Apr 09 '20 at 05:55
  • Thanks. I know about the `different flavors of netcat` but I guess they probably provide similar functionalities. Could you cook up a solution for my problem with `socat` and put in the answer? That'd really help me and I'll be very happy to accept it right away. – Tran Triet Apr 09 '20 at 06:03

1 Answers1

1

I know a way with socat:

socat TCP-LISTEN:3000,fork SYSTEM:'sleep 2; cat httprespose',pty,echo=0

Roughly based on my another answer.

Kamil Maciorowski
  • 19,242
  • 1
  • 50
  • 94