简体   繁体   English

如何TCPServer从Ruby中的http请求接收数据?

[英]How to TCPServer receive data from http request in Ruby?

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 在这一点上,似乎我仍然可以在curl中输入一些东西,所以我假设tcp连接是双向的,我输入

data from the curl client 

after that, how can I receive this message from curl client in the current TCPSocket instance? 之后,如何从当前TCPSocket实例中的curl客户端收到此消息?

I've tried 我试过了

socket.read

but it doesn't work 但它不起作用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM