19

I've been trying to use socat to respond on each connection to a socket it's listening to with a fake HTTP reply. I cannot get it working. It might be because I'm using the cygwin version of socat? I don't know.

Part of the problem is I want the second argument <some_file_response> not to be written to. In other words because it's bidirectional it will read what's in response.txt and then write to that same file, and I don't want that. Even if I do open:response.txt,rdonly it doesn't work repeatedly. system: doesn't seem to do anything. exec seems like it works, for example I can do exec:'cat response.txt' but that never gets sent to the client connecting to port 1234.

socat -vv tcp-listen:1234,reuseaddr,fork <some_file_response>

I want it to read a file to the client that's connected and then close the connection, and do that over and over again (that's why I used fork).

I am putting a bounty on this question. Please only give me solutions that work with the cygwin version from the windows command prompt.

loop
  • 3,460
  • 5
  • 34
  • 57

3 Answers3

13

Tested with cygwin:

socat -v -v TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat <some_file_response>"

If you do not want a complete HTTP response, leave out the echos:

socat -v -v TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"cat <some_file_response>"
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Jack Miller
  • 6,843
  • 3
  • 48
  • 66
4

Taken from socat examples

socat -vv TCP-LISTEN:8000,crlf,reuseaddr,fork SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat"
flotto
  • 535
  • 5
  • 17
  • 1
    This command just echoes back what the client types. (Just as hinted in the examples: "a very primitive HTTP/1.0 echo server"). – Jack Miller May 06 '15 at 05:54
2

This one works:

socat -v -v -d -d TCP-LISTEN:8080,reuseaddr,fork exec:"cat http.response",pipes

Two things need to be aware,

  • should you add crlf, as in other answers. I recommend not.

    1. crlf caused problem sending image
    2. just use \r\n explicitly in http response headers.
  • without pipes, seems no data sent to client. browser complains:

    127.0.0.1 didn’t send any data. ERR_EMPTY_RESPONSE

tested in cygwin.

== EDIT ==

If you want use inside cmd.exe, make sure PATH is correctly set, so that socat and cat can be found.

Say both socat.exe and cat.exe located under E:\cygwin64\bin

set PATH=%PATH%;E:\cygwin64\bin

Works in cmd.exe, with socat & cat from cygwin.

qeatzy
  • 1,363
  • 14
  • 21