简体   繁体   English

如何在 Ruby 的 Unix 上创建持久网络套接字?

[英]How do I make persistent network sockets on Unix in Ruby?

我希望能够编写一个 ruby​​ 程序,该程序可以在不丢失套接字连接的情况下重新启动。

This program gets Google's homepage and then when you pass it SIG_INT via Ctrl - C it restarts the program and reads the output of the homepage from the open socket with Google.该程序获取 Google 的主页,然后当您通过Ctrl - C传递 SIG_INT 时,它会重新启动程序并从 Google 的打开套接字读取主页的输出。

#!/usr/bin/ruby
#simple_connector.rb
require 'socket'

puts "Started."

if ARGV[0] == "restart"
  sock = IO.open(ARGV[1].to_i)
  puts sock.read
  exit
else
  sock = TCPSocket.new('google.com', 80)
  sock.write("GET /\n")
end

Signal.trap("INT") do
  puts "Restarting..."
  exec("ruby simple_connector.rb restart #{sock.fileno}")
end

while true
  sleep 1
end

You're talking about network sockets, not UNIX sockets I assume?你说的是网络套接字,而不是我假设的 UNIX 套接字?

I'm not sure this suits your needs, but the way I would do it is by seperating the networking and logic part, and only restart the logic part, then reconnect the logic part to the networking part.我不确定这是否适合您的需求,但我的做法是将网络和逻辑部分分开,只重新启动逻辑部分,然后将逻辑部分重新连接到网络部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM