Using the least resources possible, what would be

2019-08-28 16:57发布

问题:

I want to create a page that simulates a hung/frozen web page. For example, I could use a really long "sleep" in PHP. But if I wanted to make this a public tool, I could well imagine this might eat up server resource (sockets, memory, etc - I'm not that experienced at this level of abstraction) and eventually cause real problems for the server.

I don't want to simply close the socket with the client, because that would not provide the type of "waiting" behavior I want to simulate.

The solution doesn't have to be PHP related. That was just an example. It can be any language and/or web server. The only criteria is FOSS on Linux.

回答1:

You can simply use netcat to listen on a port and return nothing.

nc -l localhost 8080

Or if you wanted it to continue listening when the client has closed the connection

while (TRUE); do nc -l localhost 8080; done

edit: some versions of nc have the -k option to force netcat to continue listening after the socket is closed. In those cases you don't need to loop.