当前位置: 移动技术网 > IT编程>脚本编程>Ruby > Ruby中的Socket编程简单入门

Ruby中的Socket编程简单入门

2017年12月08日  | 移动技术网IT编程  | 我要评论

 ruby提供了两个访问级别的网络服务。在一个较低的水平,可以访问底层的操作系统,它可以实现面向连接和无连接协议的客户端和服务器支持基本的socket。

ruby也具有程序库,提供更高级别的访问特定的应用程序级的网络协议,如ftp,http等。

这篇教程介绍 ruby socket编程概念及讲解一个简单的实例。
什么是sockets?

套接字是一个双向通信信道的端点。socket能在一个进程,进程在同一台机器之间,或在不同的机器上的进程之间的进行通信。

套接字可实施过许多不同类型的通道:unix主控套接字,tcp,udp等等。套接字库提供了处理,其余的用于处理常见的传输,以及作为一个通用的接口的具体类。

套接字相关名词术语:

2015513102934767.jpg (592×667)

 一个简单的客户端:

在这里,我们将编写一个非常简单的客户端程序,这将打开一个连接到一个给定的端口和主机。 ruby的tcpsocket类提供open函数打开一个套接字。

tcpsocket.open(hosname, port ) 打开一个 tcp 链接到 hostname 在端口 port.

一旦有一个套接字打开,就可以读它像任何io对象一样。完成后记得要关闭它,因为就像需要关闭一个文件。

下面的代码是一个非常简单的客户端连接到一个给定的主机和端口,从套接字读取任何可用的数据,然后退出:

require 'socket'   # sockets are in standard library

hostname = 'localhost'
port = 2000

s = tcpsocket.open(host, port)

while line = s.gets  # read lines from the socket
 puts line.chop   # and print with platform line terminator
end
s.close        # close the socket when done

一个简单的服务器:

要写入互联网服务器,我们使用 tcpserver 类。 tcpserver 对象是一个工厂来创建 tcpsocket对象。

现在调用tcpserver.open(hostname, port 函数指定一个端口为您服务,并创建一个 tcpserver 对象。

接下来,调用accept方法返回 tcpserver 对象。此方法将等待客户端连接到指定的端口,然后返回一个表示连接到该客户端的tcpsocket对象。

require 'socket'        # get sockets from stdlib

server = tcpserver.open(2000) # socket to listen on port 2000
loop {             # servers run forever
 client = server.accept    # wait for a client to connect
 client.puts(time.now.ctime) # send the time to the client
 client.puts "closing the connection. bye!"
 client.close         # disconnect from the client
}

现在运行在后台服务器,然后运行上面的客户端看到的结果。
多客户端tcp服务器:

大多数internet上的服务器被设计来处理在任何一个时间大量的客户请求。

ruby的 thread 类可以轻松创建多线程服务器。接受请求,并立即创建一个新的执行线程来处理连接,同时允许主程序等待更多的连接:

require 'socket'        # get sockets from stdlib

server = tcpserver.open(2000)  # socket to listen on port 2000
loop {             # servers run forever
 thread.start(server.accept) do |client|
  client.puts(time.now.ctime) # send the time to the client
 client.puts "closing the connection. bye!"
  client.close        # disconnect from the client
 end
}

在这个例子中有固定循环,并当server.accept作出响应并立即创建并启动一个新的线程来处理连接,使用连接对象传递到线程。主程序紧接循环返回,并等待新的连接。

这种方式意味着使用ruby线程代码是可移植的以同样的方式将运行在linux,os x和windows。
一个微小的web浏览器:

我们可以使用套接字库实现任何互联网协议。例如,代码中获取内容的网页:

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
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

要实现类似的web客户端,可以使用一个预构建库,如 net::http 与 http 一起工作。下面是代码,这是否就相当于之前的代码:

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

请检查类似的库,ftp,smtp,pop,imap协议。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网