mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niklas Therning <nik...@trillian.se>
Subject Re: Problem sending message length in ByteBuffer
Date Tue, 04 Dec 2007 07:51:29 GMT
guttikonda wrote:
> Hi
>  
>    I am new to Mina.I have implemented a mina server to accept connections
> from tcp/ip clients.The client only accepts network byte order messages with
> message length.I figured that i can achieve it by using DataInputStream and
> DataOutputStream readUTF() and writeUTF() methods respectively.
>
> When receiving data from client i am successful in getting the correct one:
>                                 ByteBuffer rb = (ByteBuffer)msg;		
> 		reader = new DataInputStream(rb.asInputStream());	
> 		String inputMsg = reader.readUTF();
>
> I want to achieve the same thing when sending the response.I have no idea
> how i should wrap the DataOutputStream in my ByteBuffer to write it back to
> the session.If i simply write my response string back it is not producing
> the message length.Can anyone please help?
>
>  
>   
Maybe something like

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(baos);
writer.writeUTF(msg);
session.write(ByteBuffer.wrap(baos.toByteArray());

would work?

BUT, you couldn't you simply use ByteBuffer.getPrefixedString(...) and 
ByteBuffer.putPrefixedString(...) instead?

AND, you should be aware that the network layer may decide to deliver 
your message in several packets. The result is that you cannot assume 
that a ByteBuffer passed in to your IoHandler.messageReceived() method 
contains a complete message. You should look into implementing a custom 
ProtocolDecoder. There's a tutorial on this. The beauty of using 
decoder/encoder is that you will only have to deal with Strings in your 
IoHandler.

HTH
Niklas


Mime
View raw message