Socket .readLine() proble.

S

Sanny

I have a socket which reads messages.

BufferedReader br = new BufferedReader(new
InputStreamReader(MyClient.getInputStream()));

Now I get responses from the server.

I use

String message = br.readLine();

When there is no messages message should be equal to null.

But my Program hangs when message = null.

I tried

while((message = (String)bread.readLine()) != null){

}

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?

Bye
Sanny
 
G

Gordon Beaton

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?

There is no "null" message. BufferedReader.readLine() returns null to
indicate that you have reached the end of the data stream (i.e. the
remote has closed).

At all other times, readLine() will block until there is a complete
line (i.e. a message terminated by a newline).

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.

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.

/gordon

--
 
S

Sanny

There is no "null" message. BufferedReader.readLine() returns null to
indicate that you have reached the end of the data stream (i.e. the
remote has closed).

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
 
G

Gordon Beaton

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

--
 
S

Sanny

That's one way. You could also use PrintWriter.println("Hello"), which
will add a newline for you.


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
 
G

Gordon Beaton

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"?

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


--
 
S

Sanny

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

--

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
 
G

Gordon Beaton

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

--
 
S

Sanny

Sanny skrev:




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
 
S

Sanny

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

--

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
 
B

Bent C Dalager

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
 
G

Gordon Beaton

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

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top