2006-12-21
Three things hardly anyone wants to know about me
Thank you, Axel. What can I say?
When I helped in the development of the Sather compiler, I analyzed and fixed bugs purely by studying the code - my machine was way too small to bootstrap the compiler.
I met my wife at Marcello's in the Castro district in San Francisco. With funky shoes and hair she thought I was gay and thus safe to talk to - it turned out I wasn't gay but european.
The first linux I installed came by mail-order and was a stack of 3.5'' wrapped in packing tape. I wanted it because of xeyes.
2006-12-18
NIO & IP Multicast
The common wisdom: the two of them don't go together. Some official Java version in 2008 might have support, some open source derivative earlier. For those of us that need to do multicast today, here's the version that works for me with 5.0u9, Win XP, IPV4. CAVEAT: ugliness reigns and this will most certainly break with newer or non-Sun versions.
In short: we wrap the DatagramChannel's fd in a manually created PlainDatagramSocketImpl, join the multicast group and let go of the socket again:
// UGLY UGLY HACK: multicast support for NIO // create a temporary instanceof PlainDatagramSocket, set its fd and configure it Constructor<? extends DatagramSocketImpl> c = (Constructor<? extends DatagramSocketImpl>) Class.forName("java.net.PlainDatagramSocketImpl").getDeclaredConstructor(); c.setAccessible(true); DatagramSocketImpl socketImpl = c.newInstance(); Field channelFd = Class.forName("sun.nio.ch.DatagramChannelImpl").getDeclaredField("fd"); channelFd.setAccessible(true); Field socketFd = DatagramSocketImpl.class.getDeclaredField("fd"); socketFd.setAccessible(true); socketFd.set(socketImpl, channelFd.get(channel)); try { Method m = DatagramSocketImpl.class.getDeclaredMethod("joinGroup", SocketAddress.class, NetworkInterface.class); m.setAccessible(true); m.invoke(socketImpl, socketAddress, networkInterface); } finally { // important, otherwise the fake socket's finalizer will nuke the fd socketFd.set(socketImpl, null); }
Enjoy or let me know why this is a bad idea.