Ruby中的Socket編程簡單入門
Ruby提供了兩個訪問級別的網(wǎng)絡(luò)服務(wù)。在一個較低的水平,可以訪問底層的操作系統(tǒng),它可以實現(xiàn)面向連接和無連接協(xié)議的客戶端和服務(wù)器支持基本的socket。
Ruby也具有程序庫,提供更高級別的訪問特定的應(yīng)用程序級的網(wǎng)絡(luò)協(xié)議,如FTP,HTTP等。
這篇教程介紹 Ruby Socket編程概念及講解一個簡單的實例。
什么是Sockets?
套接字是一個雙向通信信道的端點。socket能在一個進程,進程在同一臺機器之間,或在不同的機器上的進程之間的進行通信。
套接字可實施過許多不同類型的通道:Unix主控套接字,TCP,UDP等等。套接字庫提供了處理,其余的用于處理常見的傳輸,以及作為一個通用的接口的具體類。
套接字相關(guān)名詞術(shù)語:
一個簡單的客戶端:
在這里,我們將編寫一個非常簡單的客戶端程序,這將打開一個連接到一個給定的端口和主機。 Ruby的TCPSocket類提供open函數(shù)打開一個套接字。
TCPSocket.open(hosname, port ) 打開一個 TCP 鏈接到 hostname 在端口 port.
一旦有一個套接字打開,就可以讀它像任何IO對象一樣。完成后記得要關(guān)閉它,因為就像需要關(guān)閉一個文件。
下面的代碼是一個非常簡單的客戶端連接到一個給定的主機和端口,從套接字讀取任何可用的數(shù)據(jù),然后退出:
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
一個簡單的服務(wù)器:
要寫入互聯(lián)網(wǎng)服務(wù)器,我們使用 TCPServer 類。 TCPServer 對象是一個工廠來創(chuàng)建 TCPSocket對象。
現(xiàn)在調(diào)用TCPServer.open(hostname, port 函數(shù)指定一個端口為您服務(wù),并創(chuàng)建一個 TCPServer 對象。
接下來,調(diào)用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 }
現(xiàn)在運行在后臺服務(wù)器,然后運行上面的客戶端看到的結(jié)果。
多客戶端TCP服務(wù)器:
大多數(shù)Internet上的服務(wù)器被設(shè)計來處理在任何一個時間大量的客戶請求。
Ruby的 Thread 類可以輕松創(chuàng)建多線程服務(wù)器。接受請求,并立即創(chuàng)建一個新的執(zhí)行線程來處理連接,同時允許主程序等待更多的連接:
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 }
在這個例子中有固定循環(huán),并當server.accept作出響應(yīng)并立即創(chuàng)建并啟動一個新的線程來處理連接,使用連接對象傳遞到線程。主程序緊接循環(huán)返回,并等待新的連接。
這種方式意味著使用Ruby線程代碼是可移植的以同樣的方式將運行在Linux,OS X和Windows。
一個微小的Web瀏覽器:
我們可以使用套接字庫實現(xiàn)任何互聯(lián)網(wǎng)協(xié)議。例如,代碼中獲取內(nèi)容的網(wǎng)頁:
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
要實現(xiàn)類似的web客戶端,可以使用一個預(yù)構(gòu)建庫,如 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
請檢查類似的庫,F(xiàn)TP,SMTP,POP,IMAP協(xié)議。
相關(guān)文章
Ruby創(chuàng)建“關(guān)鍵字”同名方法別名的方法
這篇文章主要介紹了Ruby創(chuàng)建“關(guān)鍵字”同名方法別名的方法,本文提示的是一個小技巧,特殊場景時可能會用到,需要的朋友可以參考下2015-01-01Ruby中的方法(函數(shù))學(xué)習(xí)總結(jié)
這篇文章主要介紹了Ruby中的方法(函數(shù))學(xué)習(xí)總結(jié),本文講解了函數(shù)的命名規(guī)則、函數(shù)參數(shù)、返回值等內(nèi)容,需要的朋友可以參考下2014-11-11在Ruby on Rails中使用Rails Active Resource的教程
這篇文章主要介紹了在Ruby on Rails中使用Rails Active Resource的教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04ruby判斷一個數(shù)是否為質(zhì)數(shù)(素數(shù))示例
這篇文章主要介紹了ruby判斷一個數(shù)是否為質(zhì)數(shù)(素數(shù))示例,需要的朋友可以參考下2014-05-05Ruby on Rails中Rack中間件的基礎(chǔ)學(xué)習(xí)教程
Rack是一個連接Ruby程序與服務(wù)器程序之間的中間件,甚至可以說Rails也是在Rack的基礎(chǔ)上建立起來的,這里我們就來為大家?guī)鞷uby on Rails中Rack中間件的基礎(chǔ)學(xué)習(xí)教程2016-06-06