How to listen for UDP broadcast packets on a specific network interface
-
I have a host with two network interfaces on two independent networks. I want to recieve UDP broadcast packets that are coming from one of the two networks - I don't want to recieve any UDP broadcasts that might be on the other network. The broadcast packets that I want to recieve have a destination address of 255.255.255.255 - they are *not* subnet directed broadcasts. This is pretty easy to do with *multicast* packets, but I've been unable to find a way to bind a UDP listen to a specific interface for recieving broadcast packets. One idea was to check the source address of incoming packets to see which network they came from, but this will not work becuase a sender could spoof the system by forging a source address (the network from which I do not want to recieve broadcasts is non-secure). Platform is Java 1.4 on Linux and/or Win2k.
-
Answer:
The builtin class MulticastSocket [1] should satisfy your needs. It's an extension of DatagramSocket [2] that adds several features, two of which you'll need to accomplish your goal. Your question states that you're trying to use the broadcast address "255.255.255.255", so we'll create a socket that's ready to receive and send packets on the "multicast group" 255.255.255.255. // basic multicast datagram socket. MulticastSocket socket = new MulticastSocket(); Next, we use joinGroup [3] to join the multicast group on a given interface; this limits the broadcasts received to a specific interface, as desired. // DatagramPacket.send() also takes this network address. InetAddress broadcast = InetAddress.getByName("255.255.255.255"); // get the interface object for interface "1.2.3.4". NetworkInterfaceAddr ifaddr = InetAddress.getByName("1.2.3.4"); NetworkInterface if = NetworkInterface.getByInetAddress(ifaddr); // bind the socket's receiving side to "255.255.255.255", on interface "1.2.3.4". socket.joinGroup(broadcast, if); If you like, you can choose the outgoing interface for given broadcast packets as well. // bind the socket's sending side to interface "1.2.3.4". socket.setNetworkInterface(if); Further information on the classes used above can be found at Sun's J2SE 1.4 reference [4]; I suggest browsing the methods provided in classes NetworkInterface [5] and MulticastSocket [1]. [1] http://java.sun.com/j2se/1.4/docs/api/java/net/MulticastSocket.html [2] http://java.sun.com/j2se/1.4/docs/api/java/net/DatagramInterface.html [3] http://java.sun.com/j2se/1.4/docs/api/java/net/MulticastSocket.html#joinGroup(java.net.SocketAddress,%20java.net.NetworkInterface) [4] http://java.sun.com/j2se/1.4/docs/api/overview-summary.html [5] http://java.sun.com/j2se/1.4/docs/api/java/net/NetworkInterface.html Much of the information provided in this answer was found through the following Google search [6] and result URL [7], once I realized that MulticastSocket could be used to send and receive on the broadcast address. [6] ://www.google.com/search?q=multicastsocket+%22255.255.255.255%22 [7] http://www.cee.hw.ac.uk/~mjc/teaching/3ne3/5/13+14.htm
bigjosh-ga at Google Answers Visit the source
Related Q & A:
- How to add a contact to a specific list?Best solution by Stack Overflow
- How do I check a type is of a specific template?Best solution by Stack Overflow
- How can I send an http request at a specific time?Best solution by Stack Overflow
- How to convert a video to a specific format?Best solution by Super User
- How to center a label on a specific position?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.