Question

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

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top