EarthShaker
EarthShaker

Reputation: 1

how to use netty to transmit the large byte stream(like a image) in a socket service

My server provides socket service for the client. My server's DecoderHandler extends from FrameDecoder. When my client sends small data (bytes less than about 1024) to the server, the server decodes it successfully, but when my client try to write the content of an image to the server, there comes up the index-out-bound-exception.

I guess the buffer chosen by decoder must have a limitation of size. So I printed out the parameter——buffer's class.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
        ChannelBuffer buffer) throws Exception {
    System.out.println(buffer.getClass());
    return null;
}

It turned out to be :

class org.jboss.netty.buffer.BigEndianHeapChannelBuffer

I would like to ask:could i change the buffer's type from BigEndianHeapChannelBuffer to DynamicChannelBuffer,so that I can transmit the image by socket.Or is there anyone can provide other valid methods to help solve my "transmitting big data through socket with netty" problem.

I really need your opinions.Thanks again!


The stacktrace from server:

java.lang.IndexOutOfBoundsException
at org.jboss.netty.buffer.AbstractChannelBuffer.checkReadableBytes(AbstractChannelBuffer.java:657)
at org.jboss.netty.buffer.AbstractChannelBuffer.readBytes(AbstractChannelBuffer.java:337)
at org.jboss.netty.buffer.AbstractChannelBuffer.readBytes(AbstractChannelBuffer.java:343)
at com.netty.www.codec.MessageDecoder.receiveChatInfoBeanFromClient(MessageDecoder.java:422)
at com.netty.www.codec.MessageDecoder.decode(MessageDecoder.java:70)
at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:282)
at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:216)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:274)
at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:261)
at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:349)
at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:280)
at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:200)
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:619)
12/09/26 09:12:09 INFO server.MessageServerHandler: The user's channel is closed:

The stacktrace from client:

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.write(Unknown Source)
at neusoft.socket.TCPClient.writeBytes(TCPClient.java:217)
at neusoft.socket.TestChatNew.sendChartMedia(TestChatNew.java:185)
at neusoft.socket.TestChatNew.main(TestChatNew.java:36)

Upvotes: 0

Views: 1309

Answers (2)

Norman Maurer
Norman Maurer

Reputation: 23567

From the stacktrace its clear thats a bug in your handler. You try to access more bytes then are present at the time in com.netty.www.codec.MessageDecoder.receiveChatInfoBeanFromClient

You need to check if enough bytes are readable via "ChannelBuffer.readableBytes()" and if not return null, so that it will try to invoke your method again if more bytes are received. Check the apidocs for FrameDecoder for more details.

Upvotes: 1

Norman Maurer
Norman Maurer

Reputation: 23567

Please include the stacktrace as a IndexOutOfBoundsException sounds like a bug to me.. Also include the netty version etc .

Upvotes: 0

Related Questions