tags:

views:

21

answers:

1

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.

+1  A: 

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