Ruby中的Socket編程簡單入門

來源:互聯網
上載者:User

Ruby中的Socket編程簡單入門

   這篇文章主要介紹了Ruby中的Socket編程簡單入門,是Ruby網路編程學習中的基礎知識,需要的朋友可以參考下

  Ruby提供了兩個存取層級的網路服務。在一個較低的水平,可以訪問底層的作業系統,它可以實現連線導向和無連線協定的用戶端和伺服器支援基本的socket。

  Ruby也具有程式庫,提供更進階別的訪問特定的應用程式級的網路通訊協定,如FTP,HTTP等。

  這篇教程介紹 Ruby Socket編程概念及講解一個簡單的執行個體。

  什麼是Sockets?

  通訊端是一個雙向通訊通道的端點。socket能在一個進程,進程在同一台機器之間,或在不同的機器上的進程之間的進行通訊。

  通訊端可實施過許多不同類型的通道:Unix主控通訊端,TCP,UDP等等。通訊端庫提供了處理,其餘的用於處理常見的傳輸,以及作為一個通用的介面的具體類。

  通訊端相關名詞術語:

  一個簡單的用戶端:

  在這裡,我們將編寫一個非常簡單的用戶端程式,這將開啟一個串連到一個給定的連接埠和主機。 Ruby的TCPSocket類提供open函數開啟一個通訊端。

  TCPSocket.open(hosname, port ) 開啟一個 TCP 連結到 hostname 在連接埠 port.

  一旦有一個通訊端開啟,就可以讀它像任何IO對象一樣。完成後記得要關閉它,因為就像需要關閉一個檔案。

  下面的代碼是一個非常簡單的用戶端串連到一個給定的主機和連接埠,從通訊端讀取任何可用的資料,然後退出:

  ?

1

2

3

4

5

6

7

8

9

10

11

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對象。

  ?

1

2

3

4

5

6

7

8

9

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 類可以輕鬆建立多線程伺服器。接受請求,並立即建立一個新的執行線程來處理串連,同時允許主程式等待更多的串連:

  ?

1

2

3

4

5

6

7

8

9

10

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瀏覽器:

  我們可以使用通訊端庫實現任何互連網協議。例如,代碼中擷取內容的網頁:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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.0rnrn"

 

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("rnrn", 2)

print body # And display it

  要實作類別似的web用戶端,可以使用一個預構建庫,如 Net::HTTP 與 HTTP 一起工作。下面是代碼,這是否就相當於之前的代碼:

  ?

1

2

3

4

5

6

7

8

9

10

11

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協議。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.