Ruby:将侦听套接字绑定到特定接口
我想创建一个只在特定接口(比如eth0)上侦听的TCP套接字。我该怎么做?我曾尝试浏览Socket API,但我可能没有正确理解 以下是我目前的倾听方法:Ruby:将侦听套接字绑定到特定接口,ruby,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: #{res
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)
的输出之外,没有办法按名称指定接口。谢谢,这很有效。我做了以下操作:socket=TCPServer.new(“127.0.0.1”,$port)对于记录,TCPServer.open的工作方式似乎完全相同。