欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ruby中的Socket編程簡(jiǎn)單入門

 更新時(shí)間:2015年05月13日 10:31:06   投稿:goldensun  
這篇文章主要介紹了Ruby中的Socket編程簡(jiǎn)單入門,是Ruby網(wǎng)絡(luò)編程學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

 Ruby提供了兩個(gè)訪問級(jí)別的網(wǎng)絡(luò)服務(wù)。在一個(gè)較低的水平,可以訪問底層的操作系統(tǒng),它可以實(shí)現(xiàn)面向連接和無連接協(xié)議的客戶端和服務(wù)器支持基本的socket。

Ruby也具有程序庫(kù),提供更高級(jí)別的訪問特定的應(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í)施過許多不同類型的通道:Unix主控套接字,TCP,UDP等等。套接字庫(kù)提供了處理,其余的用于處理常見的傳輸,以及作為一個(gè)通用的接口的具體類。

套接字相關(guān)名詞術(shù)語:

2015513102934767.jpg (592×667)

 一個(gè)簡(jiǎn)單的客戶端:

在這里,我們將編寫一個(gè)非常簡(jiǎn)單的客戶端程序,這將打開一個(gè)連接到一個(gè)給定的端口和主機(jī)。 Ruby的TCPSocket類提供open函數(shù)打開一個(gè)套接字。

TCPSocket.open(hosname, port ) 打開一個(gè) TCP 鏈接到 hostname 在端口 port.

一旦有一個(gè)套接字打開,就可以讀它像任何IO對(duì)象一樣。完成后記得要關(guān)閉它,因?yàn)榫拖裥枰P(guān)閉一個(gè)文件。

下面的代碼是一個(gè)非常簡(jiǎn)單的客戶端連接到一個(gè)給定的主機(jī)和端口,從套接字讀取任何可用的數(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

一個(gè)簡(jiǎn)單的服務(wù)器:

要寫入互聯(lián)網(wǎng)服務(wù)器,我們使用 TCPServer 類。 TCPServer 對(duì)象是一個(gè)工廠來創(chuàng)建 TCPSocket對(duì)象。

現(xiàn)在調(diào)用TCPServer.open(hostname, port 函數(shù)指定一個(gè)端口為您服務(wù),并創(chuàng)建一個(gè) TCPServer 對(duì)象。

接下來,調(diào)用accept方法返回 TCPServer 對(duì)象。此方法將等待客戶端連接到指定的端口,然后返回一個(gè)表示連接到該客戶端的TCPSocket對(duì)象。

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)在運(yùn)行在后臺(tái)服務(wù)器,然后運(yùn)行上面的客戶端看到的結(jié)果。
多客戶端TCP服務(wù)器:

大多數(shù)Internet上的服務(wù)器被設(shè)計(jì)來處理在任何一個(gè)時(shí)間大量的客戶請(qǐng)求。

Ruby的 Thread 類可以輕松創(chuàng)建多線程服務(wù)器。接受請(qǐng)求,并立即創(chuàng)建一個(gè)新的執(zhí)行線程來處理連接,同時(shí)允許主程序等待更多的連接:

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
}

在這個(gè)例子中有固定循環(huán),并當(dāng)server.accept作出響應(yīng)并立即創(chuàng)建并啟動(dòng)一個(gè)新的線程來處理連接,使用連接對(duì)象傳遞到線程。主程序緊接循環(huán)返回,并等待新的連接。

這種方式意味著使用Ruby線程代碼是可移植的以同樣的方式將運(yùn)行在Linux,OS X和Windows。
一個(gè)微小的Web瀏覽器:

我們可以使用套接字庫(kù)實(shí)現(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

要實(shí)現(xiàn)類似的web客戶端,可以使用一個(gè)預(yù)構(gòu)建庫(kù),如 Net::HTTP 與 HTTP 一起工作。下面是代碼,這是否就相當(dāng)于之前的代碼:

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

請(qǐng)檢查類似的庫(kù),F(xiàn)TP,SMTP,POP,IMAP協(xié)議。

 

相關(guān)文章

最新評(píng)論