4

It's handy to send server response to the client by

server = TCPServer.open 1234

socket = server.accept

socket.puts 'data from server side'

and in the client side, curl in this case

curl -v localhost:1234

*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 1234 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:1234
> Accept: */*
> 
data from server side

at this point, it seems I can still input something in the curl, so I assume the tcp connection is bidirectional, I input

data from the curl client 

after that, how can I receive this message from curl client in the current TCPSocket instance?

I've tried

socket.read

but it doesn't work

mko
  • 21,334
  • 49
  • 130
  • 191

1 Answers1

6

Something like this would read a greeting to a user, then wait for user input. This example simply echoes the input back on the server side.

server = TCPServer.open(8000)
socket = server.accept
socket.puts 'Hello!' # Prints Hello! to the client
while line = socket.gets
  puts line # Prints whatever the client enters on the server's output
end
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • GET / HTTP/1.1 User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 Host: localhost:1234 Accept: */* – mko Apr 17 '12 at 16:20
  • Above is the output I got, there is no client input message – mko Apr 17 '12 at 16:21
  • I was using `telnet localhost 8000`. I'm not sure `curl` supports two-way communication (yes, the shell looks like you can type something in, but that's not being sent to the server as far as I know). – Dylan Markow Apr 17 '12 at 17:58
  • Thanks Dylan it seems curl only can make http request, incapable of holding a tcp socket – mko Apr 20 '12 at 07:05