作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是Ruby套接字的新手。我在服务器中有项目,并且在服务器中坐下了套接字,如下所示:
require 'socket'
server = TCPServer.open(2000)
loop {
Thread.start(server.accept) do |client|
client.puts(Time.now.ctime)
client.puts "Closing the connection. Bye!"
client.close
end
require 'socket'
hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)
while line = s.gets
puts line.chop
puts "hey"
# call helloWorldMethod()
end
s.close
最佳答案
require 'socket'
server = TCPServer.open(2000)
loop do
Thread.start(server.accept) do |client|
client.puts(Time.now.ctime)
client.puts "Closing the connection. Bye!"
client.close
end
end
A Tiny Web Browser
We can use the socket library to implement any Internet protocol. Here, for example, is a code to fetch the content of a web page
www.yourwebsite.com/users
require 'socket'
host = 'www.tutorialspoint.com' # The web server
port = 80 # Default HTTP port
path = "/index.htm" # The file we want
This is the HTTP request we send to fetch a file
post
或
put
请求中更改您的请求
request = "GET #{path} HTTP/1.0\r\n\r\n"
socket = TCPSocket.open(host,port) # Connect to server
socket.print(request) # Send request
response = socket.read # Read complete response
# Split response at first blank line into headers and body
headers,body = response.split("\r\n\r\n", 2)
print body # And display it
To implement the similar web client, you can use a pre-built library like Net::HTTP for working with HTTP. Here is the code that does the equivalent of the previous code −
require 'net/http' # The library we need
host = 'www.tutorialspoint.com' # The web server
path = '/index.htm' # The file we want
http = Net::HTTP.new(host) # Create a connection
headers, body = http.get(path) # Request the file
if headers.code == "200" # Check the status code
print body
else
puts "#{headers.code} #{headers.message}"
end
关于ruby-on-rails - Ruby Socket-通过Socker运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47615000/
我正在使用 socket.io 开发一个 Node 项目,但是当发出消息时,它仅发送到发送者,而不发送到其他连接的客户端。请问我做错了什么? 下面是我的套接字服务器和客户端代码。 Node -inde
我想在端口上打开 docker,所以我使用了这个命令: service docker stop docker -H tcp://0.0.0.0:2375 -H unix:///var/run/dock
我是一名优秀的程序员,十分优秀!