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, using localhost:3000 or 127.0.0.1:3000. (I am running "rails s"and WEBrick.)
I wanted to put a software intermediary between the browser and the web server, and so began experimenting with socat. The following works just fine:
socat TCP-LISTEN:8080,fork TCP:localhost:3000
I can browse to localhost:8080 and things work as expected. However, if I omit the ",fork" argument like so,
socat TCP-LISTEN:8080 TCP:localhost:3000
the local rails web site is quite broken looking in the browser.
Why is that fork argument necessary? Why wouldn't a browser <--> web server connection work without it?
Socat is a flexible, multi-purpose relay tool. Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.
Socat is a multi-purpose networking tool which can be used in a variety of ways. It can be used as a proxy to observe the traffic of plaintext networking protocols. Socat also has built-in OpenSSL support which allows it to secure communications.
Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party. It can act like a simple ad-hoc chat program. Socat can talk to Netcat and Netcat can talk to Socat.
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:
socat
in one terminal windowsocat
instance.socat
is not listening for new connections anymore: it has moved on to servicing the one it already got.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
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With