Ruby:将侦听套接字绑定到特定接口

我想创建一个仅在特定接口(例如eth0)上侦听的TCP套接字。 我怎样才能做到这一点? 我试过浏览Socket API,但我可能无法正确理解。

到目前为止,这是我的监听方法:

def listen socket = TCPServer.open($port) while $looping do Thread.start(socket.accept) do |server| response = server.read puts "Command received: #{response}" if sanitize(response) execute(response) end end end end 

谢谢您的帮助。

您需要获取要监听的网络接口的IP,并将其作为第一个参数传递给TCPServer.new 。 除了解析%x(ifconfig | grep inet)的输出之外,我无法通过名称指定接口。