Memoirs of Fls'Zen

Monday, May 31, 2010

Java 6 & Multicasting (and UPnP)

I discovered today that Java 6 prefers that the MulticastSocket sending a DatagramPacket is the same instance that is receiving packets from the multicast group. This was discovered while I was working on implementing upnplib in a project. The library would intermittently discover the UPnP device. Maybe 1 in 20 attempts were successful. Wireshark showed that each packet was indeed going through and a response was being received. After much searching, trial, and error, I finally tried using a single multicast socket to send and receive the packets with the router. Success!

Seriously, even if you make a DatagramSocket and listen to the destination port of the response from the router, it will not reliably receive the packet. Maybe this behavior is part of the UDP specification, but I don't think so.

Since upnplib appears to be no longer maintained, I'm guessing this was something that was changed in 1.6 and the library was simply never updated to work with it. If you run into this problem, contact me and I'll tell you what I changed, but it might be more beneficial for you to do it on our own-simply have's sendSearchMessage take a parameter of DictionaryListener and in the method use the multicast socket from the listener instead of making a new one.



Post a Comment

Subscribe to Post Comments [Atom]

<< Home