Tag: tcpsocket

如何使用Ruby维护TCP连接?

使用TCPSocket ,我需要socket.puts “foobar” ,然后socket.close让套接字在另一边socket.read消息。 有没有办法通过套接字发送或接收消息,但没有关闭套接字,这意味着我可以再次发送消息而不再创建套接字? ps像websocket一样的东西

Ruby TCPSocket / HTTP请求

我刚开始使用TCPSockets。 我只想尝试获取谷歌主页。 这是我的代码: require ‘socket’ host = ‘http://www.google.com’ port = 80 s = TCPSocket.open host, port s.puts “GET / HTTP/1.1\r\n” s.puts “Host: Firefox” s.puts “Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8” s.puts “Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7” s.puts “\r\n” while line = s.gets puts line.chop end s.close 返回: HTTP/1.1 302 Document has moved Location: http://92.242.140.29/?nxdomain=http%3A%2F%2Ffirefox&AddInType=2&PlatformInfo=pbrgen 为什么? 我的目标是获取谷歌主页的内容。 谢谢