开发者

Ruby: Binding a listening socket to a specific interface

I want to create a TCP socket that listens only on a specific interface (say, eth0). How can I do this? I've tried browsing through the Socket API, but I may not be understanding things properly.

Here is my listen method so far:

def listen
  socket = TCPServer.open($port)
  while $looping do
    Thread.start(socket.accept) do |server|
      response = server.read
      puts "Command received: #{respo开发者_如何学Gonse}"
      if sanitize(response)
        execute(response)
      end
    end
  end
end

Thanks for the help.


You will need to get the IP of the network interface you want to listen on and pass it as the first parameter to TCPServer.new. There is no way that I know of to specify the interface by name besides parsing the output of %x(ifconfig <interface> | grep inet).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜