2009-02-23 66 views
0

我正在尋找基於ruby的網絡服務器通過管道而不是通過TCP/IP進行通信的可能性。所以我會通過管道發送HTTP請求,我想通過管道讀取響應。它應該用作桌面應用程序的捆綁/內部網絡服務器(RPC或其他)。當我的應用程序的更多實例在同一臺機器上運行時,我不想處理端口配置。沒有打開端口的紅寶石網絡服務器

任何想法?

預先感謝您。

回答

2

嘗試使用UNIXSocket使用本地路徑指定套接字連接的位置,而不是端口,並且您可以輕鬆處理多個同時連接。

# server.rb 
require 'socket' 
File.delete(filename) if File.exists? filename 
server = UNIXServer.open(filename) 
server.listen(queuesize) 

puts "waiting on client connection" 
while client= server.accept 
    puts "got client connection #{client.inspect}" 

    child_pid = fork do 
    puts "Asking the client what they want" 
    client.puts "Welcome to your server, what can I get for you?" 
    until client.eof? 
     line = client.gets 
     puts "The client wants #{line.chomp.inspect}" 
    end 
    client.close 
    end 

    puts "running server (#{child_pid})" 
    client.close 
    Process.detach(child_pid) 
end 

server.close 


# client.rb 
require 'socket' 
puts "requesting server connection" 
server = UNIXSocket.new(filename) 
puts "got server connection #{server}" 
line = server.gets 
puts "The server said: #{line.chomp.inspect}" 
%w{ a-pony a-puppy a-kitten a-million-dollars }.each do |item| 
    server.puts item 
end 
server.close 
0

不是你的問題的答案。但是,如果最終不得不使用TCP/IP HTTP Server,則應確保它僅在127.0.0.1上進行偵聽。監聽本地主機地址應該非常快,因爲它不會觸及網絡,並且還會阻止人們從外部進行連接,從而使其更加安全。

1

Pipe是用於單向通信的,所以你無法在其上設置網絡服務器。您可以嘗試使用unix套接字。但最簡單的解決方案是使用回送(127.0.0.1)。它的高度優化,所以速度不會成爲問題。