mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From guttikonda <divya_...@yahoo.com>
Subject Re: Problem sending message length in ByteBuffer
Date Tue, 04 Dec 2007 15:47:03 GMT

Thanks for all the suggestions.I tried using ByteArrayInputStream and it
worked.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(baos);
writer.writeUTF(msg);
session.write(ByteBuffer.wrap(baos.toByteArray());

AND, about the quote saying 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".Is it always or when using
ByteBuffer.getPrefixedString(...) method only? Please help.


Niklas Therning wrote:
> 
> 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
> 
> 
> 
> -----
> Niklas Therning
> www.spamdrain.net
> 

-- 
View this message in context: http://www.nabble.com/Problem-sending-message-length-in-ByteBuffer-tf4939850s16868.html#a14152909
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Mime
View raw message