Server reply coming through as -1

"Trustin Lee (이희승)" trustin at gmail.com
Sun Jun 6 22:34:48 EDT 2010


1) MessageEvent.getMessage() returns the received message object
(ChannelBuffer in this case probably).  If you want to write a reply,
you should create a new buffer instead of reusing the received buffer
unless there is an obvious reason.

2) You wrote 4 bytes integer length field on the server side, but you
read only 1 byte on the client side.

3) in.read() returns -1 when the connection has been closed.  Please
refer to JDK API documentation.  Maybe you need to visit Java
programming language forum first?

HTH,
Trustin

djb wrote:
> Hi,
> 
> I've written my first Netty server, which hosts a program for C++ clients... 
> It receives a request, processes and replies.  At the moment, I'm still
> testing a Java-Java version.  
> 
> I'm using the IntegerHeaderFrameDecoder from the Javadocs, and am receiving
> my client's messages.  But when I reply in my server's message handler, it
> goes ahead and sends it, and then my client receives -1.
> 
> ------------------------------------------
> Client code:
> ------------
> socket = new Socket("localhost", MultiThreadedClient.port);
> 
> BufferedOutputStream out = new BufferedOutputStream(
> 		socket.getOutputStream());
> BufferedInputStream in = new BufferedInputStream(
> 		socket.getInputStream());
> 
> byte[] data = claim.getBytes("UTF-8");
> 
> out.write( getByteInt(false, data.length));
> out.write( data ); 
> out.flush();
> System.out.println("Sent claim to server.");
> 
> ------------------------------------------
> Goes to this Server code:
> ------------
> //(receives request, sends back results)
> byte[] data = results.getXMLString().getBytes("UTF-8");
>       
> ChannelBuffer replyBuffer = (ChannelBuffer)e.getMessage();
> replyBuffer.resetWriterIndex();  //removing this causes
> ArrayOutOfBoundsException instead of -1
> replyBuffer.writeInt(data.length);
> replyBuffer.writeBytes(data);
> ----------------------------------------
> Comes back to client:
> ------------
> 
> int length = in.read();
> System.out.println("Got Length of reply: " + length); //<<<Outputs -1 here
> byte[] reply = new byte[length];
> in.read(reply);
> System.out.println(new String(reply));
> ------------------------------------------
> 
> Please advise why the client thinks it is EOF...
> 
> Thanks
> Daniel

-- 
what we call human nature in actuality is human habit
http://gleamynode.net/



More information about the netty-users mailing list