Hi,

To get the same result using MINA as you get with your plain socket
server (the one using readLine()) I suggest you have a look at the
TextLineCodec
(http://directory.apache.org/subprojects/mina/apidocs/org/apache/mina/filter/codec/textline/TextLineCodecFactory.html).
When using it MINA will automatically split the received data on \n
characters and each line will be delivered to your IoHandler seperately.
IIRC TextLineCodec will also strip the \n from the lines just like
readLine() does. Your messageReceived() method (which is roughly the
MINA 0.9 equivalent of MINA 0.8's dataRead() method) will be called 10
times just as you want and you don't have to call flush() on the client
side for each line.

Unfortunately I don't know exactly how to do the above in MINA 0.8. I
suggest you use the latest 0.9 release instead. That will make thinks a
lot simpler (at least for me if you need more help).

/Niklas

hpq852 wrote:
> I mainly want to get what is if client flush for 10 times , then the server 
> side dataRead method should be invoked 10 times, but I think it can't come 
> true. I have followed peter's idea, but it can't resolve the problem. 
>
> When I don't use NIO just use Socket then it can work well. I think the 
> reason is what in socket way the thread was be blocked by in.readLine() 
> method, if client have a flush, then the server can read the data:
>
>     while (true)
>     {
>      String str = in.readLine();
>      System.out.println(str);
>      System.out.println("-----------------");
>     }
>
> but in SocketChannel and selector way ,the selector only check if channel 
> have data coming. When data come then it will we invoke channel reading the 
> data, but in the period of this process, maybe another flush have be invoked, 
> then another ' Hello World ' have comed. So the dataRead moethod may print 
> the combined string. Is this right ?  How should I do if I want to get what I 
> want ?   Thank you . 
>
>
> hpq852
> 2006-07-24
>
>
>
> 发件人: Maarten Bosteels
> 发送时间: 2006-07-24 16:12:18
> 收件人: [email protected]
> 抄送: 
> 主题: Re: About Mina Question, Could you help me ?
>
> I  do  not  quite  understand  the  problem.  Do  you  want  every  "Hello  
> World"  to  be
> in  a  separate  TCP  packet  ?
> I  don't  think  it  is  a  good  idea  to  try  to  accomplish  that  
> behavior.
> Your  code  should  not  depend  on  the  number  of  packets  used  to  
> transfer  the
> data.
>
> Why  don't  you  use  delimiter  to  tell  the  messages  apart  ?
> Our  use  TextLineDecoder:
>
> http://directory.apache.org/subprojects/mina/apidocs/org/apache/mina/filter/codec/textline/TextLineDecoder.html
>
> btw,  any  particular  reason  you  are  still  using  MINA  0.8  ?
>
> Maarten
>
> On  7/24/06,  peter  royal   <[EMAIL PROTECTED] >  wrote:
>   
>>  On  Jul  23,  2006,  at  11:14  AM,  hpq852  wrote:
>>   >  It  alse  can't  work  well,  Now  the  client  is  :
>>   >
>>   >          public  void  run()
>>   >          {
>>   >            for(int  i=0;  i <10;  i++)
>>   >            {
>>   >              out.print("  Hello  World  \n");
>>   >              out.flush();
>>   >            }
>>   >          }
>>   >
>>   >  if  I  invode  the  flush  method  for  10  times,  I  also  want  to  
>> get  the
>>   >  dataRead(IoSession  session,  ByteBuffer  rb)  mothed  invoked
>>
>>  Try  toggling  TCP_NODELAY.  By  default,  your  TCP  stack  will  try  to
>>  combine  data  into  a  single  packet,  which  would  result  in  fewer
>>  invocations  of  dataRead.
>>
>>  -epte
>>
>>
>>  --
>>  [EMAIL PROTECTED]  -  http://fotap.org/~osi
>>
>>
>>
>>
>>
>>
>>     


-- 
Niklas Therning
Software Architect
www.spamdrain.net


Reply via email to