Ruby提供了兩個(gè)訪問(wèn)級(jí)別的網(wǎng)絡(luò)服務(wù)。在一個(gè)較低的水平,可以訪問(wèn)底層的操作系統(tǒng),它可以實(shí)現(xiàn)面向連接和無(wú)連接協(xié)議的客戶(hù)端和服務(wù)器支持基本的socket。
Ruby也具有程序庫(kù),提供更高級(jí)別的訪問(wèn)特定的應(yīng)用程序級(jí)的網(wǎng)絡(luò)協(xié)議,如FTP,HTTP等。
這篇教程介紹 Ruby Socket編程概念及講解一個(gè)簡(jiǎn)單的實(shí)例。
什么是Sockets?
套接字是一個(gè)雙向通信信道的端點(diǎn)。socket能在一個(gè)進(jìn)程,進(jìn)程在同一臺(tái)機(jī)器之間,或在不同的機(jī)器上的進(jìn)程之間的進(jìn)行通信。
套接字可實(shí)施過(guò)許多不同類(lèi)型的通道:Unix主控套接字,TCP,UDP等等。套接字庫(kù)提供了處理,其余的用于處理常見(jiàn)的傳輸,以及作為一個(gè)通用的接口的具體類(lèi)。
套接字相關(guān)名詞術(shù)語(yǔ):
一個(gè)簡(jiǎn)單的客戶(hù)端:
在這里,我們將編寫(xiě)一個(gè)非常簡(jiǎn)單的客戶(hù)端程序,這將打開(kāi)一個(gè)連接到一個(gè)給定的端口和主機(jī)。 Ruby的TCPSocket類(lèi)提供open函數(shù)打開(kāi)一個(gè)套接字。
TCPSocket.open(hosname, port ) 打開(kāi)一個(gè) TCP 鏈接到 hostname 在端口 port.
一旦有一個(gè)套接字打開(kāi),就可以讀它像任何IO對(duì)象一樣。完成后記得要關(guān)閉它,因?yàn)榫拖裥枰P(guān)閉一個(gè)文件。
下面的代碼是一個(gè)非常簡(jiǎn)單的客戶(hù)端連接到一個(gè)給定的主機(jī)和端口,從套接字讀取任何可用的數(shù)據(jù),然后退出:
require 'socket' # Sockets are in standard libraryhostname = 'localhost'port = 2000s = TCPSocket.open(host, port)while line = s.gets # Read lines from the socket puts line.chop # And print with platform line terminatorends.close # Close the socket when done
一個(gè)簡(jiǎn)單的服務(wù)器:
要寫(xiě)入互聯(lián)網(wǎng)服務(wù)器,我們使用 TCPServer 類(lèi)。 TCPServer 對(duì)象是一個(gè)工廠來(lái)創(chuàng)建 TCPSocket對(duì)象。
現(xiàn)在調(diào)用TCPServer.open(hostname, port 函數(shù)指定一個(gè)端口為您服務(wù),并創(chuàng)建一個(gè) TCPServer 對(duì)象。
接下來(lái),調(diào)用accept方法返回 TCPServer 對(duì)象。此方法將等待客戶(hù)端連接到指定的端口,然后返回一個(gè)表示連接到該客戶(hù)端的TCPSocket對(duì)象。
require 'socket' # Get sockets from stdlibserver = TCPServer.open(2000) # Socket to listen on port 2000loop { # 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}
現(xiàn)在運(yùn)行在后臺(tái)服務(wù)器,然后運(yùn)行上面的客戶(hù)端看到的結(jié)果。
多客戶(hù)端TCP服務(wù)器:
大多數(shù)Internet上的服務(wù)器被設(shè)計(jì)來(lái)處理在任何一個(gè)時(shí)間大量的客戶(hù)請(qǐng)求。
Ruby的 Thread 類(lèi)可以輕松創(chuàng)建多線(xiàn)程服務(wù)器。接受請(qǐng)求,并立即創(chuàng)建一個(gè)新的執(zhí)行線(xiàn)程來(lái)處理連接,同時(shí)允許主程序等待更多的連接:
require 'socket' # Get sockets from stdlibserver = TCPServer.open(2000) # Socket to listen on port 2000loop { # 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}
在這個(gè)例子中有固定循環(huán),并當(dāng)server.accept作出響應(yīng)并立即創(chuàng)建并啟動(dòng)一個(gè)新的線(xiàn)程來(lái)處理連接,使用連接對(duì)象傳遞到線(xiàn)程。主程序緊接循環(huán)返回,并等待新的連接。
這種方式意味著使用Ruby線(xiàn)程代碼是可移植的以同樣的方式將運(yùn)行在Linux,OS X和Windows。
一個(gè)微小的Web瀏覽器:
我們可以使用套接字庫(kù)實(shí)現(xiàn)任何互聯(lián)網(wǎng)協(xié)議。例如,代碼中獲取內(nèi)容的網(wǎng)頁(yè):
require 'socket' host = 'www.tutorialspoint.com' # The web serverport = 80 # Default HTTP portpath = "/index.htm" # The file we want # This is the HTTP request we send to fetch a filerequest = "GET #{path} HTTP/1.0/r/n/r/n"socket = TCPSocket.open(host,port) # Connect to serversocket.print(request) # Send requestresponse = socket.read # Read complete response# Split response at first blank line into headers and bodyheaders,body = response.split("/r/n/r/n", 2) print body # And display it
要實(shí)現(xiàn)類(lèi)似的web客戶(hù)端,可以使用一個(gè)預(yù)構(gòu)建庫(kù),如 Net::HTTP 與 HTTP 一起工作。下面是代碼,這是否就相當(dāng)于之前的代碼:
require 'net/http' # The library we needhost = 'www.tutorialspoint.com' # The web serverpath = '/index.htm' # The file we want http = Net::HTTP.new(host) # Create a connectionheaders, body = http.get(path) # Request the fileif headers.code == "200" # Check the status code print body else puts "#{headers.code} #{headers.message}" end
請(qǐng)檢查類(lèi)似的庫(kù),F(xiàn)TP,SMTP,POP,IMAP協(xié)議。
新聞熱點(diǎn)
疑難解答
圖片精選