> This also hangs when there is no messages. It only works when there
> is a message in the BufferedReader br. When BufferedReader is emply
> String message = br.readLine(); hangs the program.
>
> Anyone knows how to know whether BufferedReader already has message or
> it is null before calling readline?
> > This also hangs when there is no messages. It only works when there
> > is a message in the BufferedReader br. When BufferedReader is emply
[quoted text clipped - 9 lines]
> At all other times, readLine() will block until there is a complete
> line (i.e. a message terminated by a newline).
I have following message to pass through socket.
String message1= "Hello";
Should I make it String message1="Hello"+"\r\n" and then send the
message through Socket?
> If you need to know that there is data before attempting to read, use
> a Selector. It's important to note that the Selector only tells you
> that there is data to read, NOT whether there is a newline at the end
> of the message, so readLine() might block anyway.
BufferedReader br;
I want to know in advance whether br.readline() is null or not. That
is "br" is having any data or not. How to use Selector.
Can you write the code how to use Selector to determing br.readline()
has something in it or not?
> If your client is well behaved (every message really does end with a
> newline) this shouldn't be a problem. If that's a problem then you can
> implement your own readLine() with the behaviour you need.
Client sends messages every 3 seconds. I find the Program hangs for 1
min and sometimes start reveiving messages after 2 minutes.
If 2-3 messages are sent to BufferReader will I have to start a
String function ping(){
do{
message = br.readline();// My Program hangs when there is no message.
(message=null.)
////
////
} While(message!=null)
return("ok")
}//function end.
Can you write how to use Selecter in above code?
I use a function ping(). Which is called every 3 seconds to look for
new messages. When there is some message comming the function works
properly. But when Message is empty it hangs.
Thankyou
Sanny
Gordon Beaton - 04 Oct 2007 12:19 GMT
> I have following message to pass through socket.
>
> String message1= "Hello";
>
> Should I make it String message1="Hello"+"\r\n" and then send the
> message through Socket?
That's one way. You could also use PrintWriter.println("Hello"), which
will add a newline for you.
> I want to know in advance whether br.readline() is null or not. That
> is "br" is having any data or not. How to use Selector.
What part of the API documentation are you having trouble
understanding?
> Can you write the code how to use Selector to determing
> br.readline() has something in it or not?
I already have a day job, thanks.
Here you'll find documentation and examples:
http://java.sun.com/javase/6/docs/technotes/guides/io/index.html
/gordon
--
Sanny - 04 Oct 2007 14:23 GMT
> That's one way. You could also use PrintWriter.println("Hello"), which
> will add a newline for you.
[quoted text clipped - 4 lines]
> What part of the API documentation are you having trouble
> understanding?
I am unable to understand how to implement Selector on BufferReader
"br". So that I get whether "br" is empty or not.
Selector sel=Selector.open()
Then I can use sel.isOpen() to know wheether the BufferReader "br" is
empty or not.
But how to Attach Selector to BufferReader "br"?
Bye
Sanny
Gordon Beaton - 04 Oct 2007 14:29 GMT
> I am unable to understand how to implement Selector on BufferReader
> "br". So that I get whether "br" is empty or not.
[quoted text clipped - 5 lines]
>
> But how to Attach Selector to BufferReader "br"?
First:
- Open a Selector.
- Get the Channel corresponding to your Socket and set it to non-blocking.
- Register the Channel with the Selector, specifying SelectionKey.OP_READ.
Then:
- Use Selector.select() to wait, or Selector.selectNow() to check for data.
- If the Selector indicates activity, get the set of SelectedKeys from it.
- For each of the keys:
- if isReadable(): read the available data
- call key.remove()
Examples here:
http://rox-xmlrpc.sourceforge.net/niotut/index.html
Google will find others.
/gordon
--
Sanny - 05 Oct 2007 12:06 GMT
> > I am unable to understand how to implement Selector on BufferReader
> > "br". So that I get whether "br" is empty or not.
[quoted text clipped - 26 lines]
>
> --
I went through all you said, It was very tough. However I got other
way to know the BufferReader is empty or not.
I used (br.ready()==true) before br.readln()
br.ready is false when buffer reader is empty. So This helped me
implement without the use of Selector.
Anyways, Thanks for your help.
Bye
Sanny
Gordon Beaton - 05 Oct 2007 13:07 GMT
> I used (br.ready()==true) before br.readln()
>
> br.ready is false when buffer reader is empty. So This helped me
> implement without the use of Selector.
Be aware that br.ready() will not be true at EOF, so you won't be able
to detect this state on your connection. Selector.select() doesn't
have this problem.
/gordon
--
Sanny - 05 Oct 2007 18:27 GMT
> > I used (br.ready()==true) before br.readln()
>
[quoted text clipped - 8 lines]
>
> --
My code is working properly. I do not go into much detail. I have
tested it. And it gives correct results without any hang.
Bye
Sanny
Lars Enderin - 05 Oct 2007 15:14 GMT
Sanny skrev:
> I used (br.ready()==true) before br.readln()
Why do you write (br.ready()==true), when (br.ready()) is sufficient and
idiomatic?
Sanny - 05 Oct 2007 18:25 GMT
> Sanny skrev:
>
> > I used (br.ready()==true) before br.readln()
>
> Why do you write (br.ready()==true), when (br.ready()) is sufficient and
> idiomatic?
I want to fully certain that br.ready() is equal to true. It helps
reading the code easily and get comfort what the code is doing.
I try to make code as simple as possible so that it is easy to debug
later.
I heard somepeople write if (i) instead of if (i>0).
Bye
Sanny
Bent C Dalager - 05 Oct 2007 19:01 GMT
>I heard somepeople write if (i) instead of if (i>0).
They don't in Java, since it won't compile.
In the languages where they do (e.g. C), if (i) means if (i!=0).
Cheers
Bent D

Signature
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs
Gordon Beaton - 08 Oct 2007 06:48 GMT
>> Sanny skrev:
>> > I used (br.ready()==true) before br.readln()
[quoted text clipped - 3 lines]
>
> I want to fully certain that br.ready() is equal to true.
Why stop there? This will test that br.ready() *REALLY* is true:
if (((br.ready() == true) == true) && ((br.ready() == true) != false))
/gordon
--