Multicasting objects with DatagramPacket and MulticastSocket not working properly

Darkie darkscyther at hotmail.com
Wed May 11 07:12:45 EDT 2011


Hey,
I'm trying to implement a multicast of my custom objects with netty. I use
this piece of code to setup the thing:

public static void main(String[] args) throws Exception {
        DatagramChannelFactory datagramChannelFactory = new
        OioDatagramChannelFactory(Executors.newCachedThreadPool());

        ConnectionlessBootstrap connectionlessBootstrap = new
        ConnectionlessBootstrap(datagramChannelFactory);
        ChannelPipeline channelPipeline =
connectionlessBootstrap.getPipeline();

        channelPipeline.addLast("handler", new MulticastReceiverHandler());

        DatagramChannel datagramChannel = (DatagramChannel)
        connectionlessBootstrap.bind(new
InetSocketAddress(MCAST_GROUP_PORT));
        InetSocketAddress multicastAddress = new
InetSocketAddress(MCAST_GROUP_IP, MCAST_GROUP_PORT);
        NetworkInterface networkInterface =
       
NetworkInterface.getByInetAddress(InetAddress.getByName(BIND_ADDRESS));
        datagramChannel.joinGroup(multicastAddress, networkInterface);

        // SEND
        InetAddress mcastGroup = InetAddress.getByName(MCAST_GROUP_IP);
        MulticastSocket mcastSocket = new MulticastSocket(MCAST_GROUP_PORT);
        mcastSocket.joinGroup(mcastGroup);
        mcastSocket.setTimeToLive(1);
        //DatagramPacket p = new DatagramPacket("HELLO".getBytes(), 5,
mcastGroup, MCAST_GROUP_PORT);
        //mcastSocket.send(p);
        
        String v = "100";
        //while(true){
        	IMessage m = new RequestMessage(v);
        	
        	ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        	ObjectOutputStream oStream = new ObjectOutputStream( bStream );
        	oStream.writeObject ( m );
        	byte[] byteVal = bStream. toByteArray();
        	
        	DatagramPacket p = new DatagramPacket(byteVal, 5, mcastGroup,
MCAST_GROUP_PORT);
        	mcastSocket.send(p);
        //}
    }


As you can see I create an instance of my message (towards the end) and
transform it in a byte[], then I create a DatagramPacket and send it through
the MulticastSocket.

At the receiver sied I do something like this:

        public void messageReceived(ChannelHandlerContext ctx, MessageEvent
e)
                throws Exception {
        	ChannelBuffer buf = (ChannelBuffer) e.getMessage(); 
            int length = buf.readableBytes(); 
             ByteBuffer nioBuf = buf.toByteBuffer(); 
             DatagramPacket packet; 
             if (nioBuf.hasArray()) { 
                 // Avoid copy if the buffer is backed by an array. 
                 packet = new DatagramPacket( nioBuf.array(),
nioBuf.arrayOffset(), length); 
                 ByteArrayInputStream b_in = new
ByteArrayInputStream(packet.getData());
                 ObjectInputStream o_in = new ObjectInputStream(b_in);
                 IMessage m = (RequestMessage) o_in.readObject();
             }
}

When something is received I get the byte[] out of it and try to transform
it into my custom message.
This code, when run, throws an exception when the last line of the receiver
is executed. The following is the stack trace.

java.io.StreamCorruptedException: invalid type code: 00
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1498)
	at
java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
	at
ch.inf.usi.netty.examples.MulticastReceiver$MulticastReceiverHandler.messageReceived(MulticastReceiver.java:92)
	at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:274)
	at
org.jboss.netty.channel.socket.oio.OioDatagramWorker.run(OioDatagramWorker.java:84)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:680)

As you can see there is a problem in the decoding.

If I try to send a String (instead of an IMessage), changing the receiver
(since String has already a byte decoder) I can read without problem the
String.

Do you know what's going on with custom objects?
Thanks a lot.

--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Multicasting-objects-with-DatagramPacket-and-MulticastSocket-not-working-properly-tp6351425p6351425.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list