3

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: #{response}"
      if sanitize(response)
        execute(response)
      end
    end
  end
end

Thanks for the help.

Magicked
  • 637
  • 3
  • 11
  • 20

1 Answers1

3

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).

Adrian
  • 14,931
  • 9
  • 45
  • 70
  • Thanks, that worked. I did the following: socket = TCPServer.new("127.0.0.1", $port) For the record, TCPServer.open seems to work the exact same way. – Magicked Jul 21 '10 at 19:44