麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Ruby > 正文

Ruby之Socket編程詳細解析 新手教程

2020-02-24 15:40:50
字體:
來源:轉載
供稿:網友

  下面這篇教程就來給大家介紹一下ruby.html" target="_blank">Ruby Socket編程概念以及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協議。

  以上就是Ruby Socket編程詳細解析了,更多相關內容請繼續關注武林技術頻道。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 精品呦女 | www.99久久久 | 久章草在线视频 | 在线看日本 | 久久久久久久午夜 | 久久毛片 | 天天鲁在线视频免费观看 | 全网免费毛片 | 久久蜜桃精品一区二区三区综合网 | 久草手机在线 | 欧美日韩在线免费观看 | 毛片一级网站 | 素人视频在线观看免费 | 一级毛片在线免费观看视频 | 亚洲欧美不卡视频 | 久久久久久久一区二区三区 | 黄色的视频在线观看 | 日本xxxx色视频在线观看免费, | 3344永久免费 | 国产成人精品一区二区三区电影 | 蜜桃视频在线入口www | 午夜精品成人一区二区 | 欧美人xx | 99激情视频| 中文字幕 亚洲一区 | 免费观看黄色影片 | 成人免费区 | 欧美十区 | 久久综合精品视频 | av在线在线 | 一级免费黄色免费片 | 黄色aaa视频 | 国产成人网 | 爱操在线 | 国产激情网 | 久久免费精品 | 2021免费日韩视频网 | 久久免费视屏 | 国产1区在线 | 亚洲网站在线 | 国产精品久久久久久久久久尿 |