require 'socket' # Sockets 是标准库 hostname = 'localhost' port = 2000 s = TCPSocket.open(hostname, port) while line = s.gets # 从 socket 中读取每行数据 puts line.chop # 打印到终端 end s.close # 关闭 socket
require 'socket' # 获取socket标准库
server = TCPServer.open(2000) # Socket 监听端口为 2000
loop { # 永久运行服务
client = server.accept # 等待客户端连接
client.puts(Time.now.ctime) # 发送时间到客户端
client.puts "Closing the connection. Bye!"
client.close # 关闭客户端连接
}
require 'socket' # 获取socket标准库
server = TCPServer.open(2000) # Socket 监听端口为 2000
loop { # 永久运行服务
Thread.start(server.accept) do |client|
client.puts(Time.now.ctime) # 发送时间到客户端
client.puts "Closing the connection. Bye!"
client.close # 关闭客户端连接
end
}
require 'socket'
host = 'www.ziqiangxuetang.com' # web服务器
port = 80 # 默认 HTTP 端口
path = "/index.htm" # 想要获取的文件地址
# 这是个 HTTP 请求
request = "GET #{path} HTTP/1.0rnrn"
socket = TCPSocket.open(host,port) # 连接服务器
socket.print(request) # 发送请求
response = socket.read # 读取完整的响应
# Split response at first blank line into headers and body
headers,body = response.split("rnrn", 2)
print body # 输出结果
require 'net/http' # 我们需要的库
host = 'www.ziqiangxuetang.com' # web 服务器
path = '/index.htm' # 我们想要的文件
http = Net::HTTP.new(host) # 创建连接
headers, body = http.get(path) # 请求文件
if headers.code == "200" # 检测状态码
print body
else
puts "#{headers.code} #{headers.message}"
end
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有