From: Tanaka Akira Date: 2009-02-07T23:47:47+09:00 Subject: Re: How do I get the destination IP of a received UDP packet? In article <4ee7ef4b9ffa58f7d0d4c81634802cbf@ruby-forum.com>, Gavin Yap writes: > I need to write a server which is able to identify client by > destinations. > > My machine is listening to "0.0.0.0", any IP addrs configured to it. > Take for instance, the machine is configured with "192.168.1.10, > 172.16.0.10", also listening to "0.0.0.0", allow listening to Broadcast > queries, which is also important to my application How about using sockets for each IP address and "0.0.0.0"? require 'socket' s0 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) s0.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) s0.bind(Socket.sockaddr_in(9999, "0.0.0.0")) s1 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) s1.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) s1.bind(Socket.sockaddr_in(9999, "192.168.1.10")) s2 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) s2.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) s2.bind(Socket.sockaddr_in(9999, "172.16.0.10")) while true rs, = IO.select([s0, s1, s2]) rs.each {|s| p [Socket.unpack_sockaddr_in(s.getsockname)[1], s.recv(100)] } end I'm not certain about the portability of the broadcast handling, though. -- Tanaka Akira