Why is “fork” needed by socat when connecting to a web server?

后端 未结 1 1377
星月不相逢
星月不相逢 2021-02-02 14:18

I am trying to understand tcp connections between a browser and a web server. I have a web server running on my local machine, and can browse to it just fine, as expected, usin

相关标签:
1条回答
  • 2021-02-02 14:51

    Without the fork, socat will accept a single TCP connection, forward data bidirectionally between the two endpoints for as long as that connection remains open, then exit. You can see this yourself easily:

    • Run socat in one terminal window
    • Telnet to localhost 8080 in another terminal window. It connects to the socat instance.
    • Telnet to localhost 8080 in a third terminal window. You get a connection refused error, because socat is not listening for new connections anymore: it has moved on to servicing the one it already got.
    • Type an HTTP request into the second terminal window. You'll get an HTTP response, and then socat will exit as the connection is closed.

    The fork option just makes it fork a new child to process the newly accepted connection while the parent goes back to waiting for new connections.

    socat's use of fork() rather than something more sophisticated like preforking or connection pooling is the reason you wouldn't want to implement high-performance middleware with socat!

    0 讨论(0)
提交回复
热议问题