1

In java there is another Object like BufferedReader to read data recived by server?? Because the server send a string without newline and the client don't print any string untile the Server close the connection form Timeout (the timeout message have a newline!) , after the Client print all message recived and the timeout message send by server! help me thanks!!

3
  • 2
    Here's the Java API do: java.sun.com/javase/6/docs/api - learn how to use it. Commented Jan 15, 2011 at 12:03
  • 2
    Michael, that's not too helpful. Approaching a whole class library or framework can be quite daunting. And if all you ever did was some small text file or console I/O (not unreasonable for beginner programming tasks), then BufferedReader might really be the only thing they know (and hey, it has a readLine() method which is definitely handy). Commented Jan 15, 2011 at 12:10
  • 2
    Joey: while Michael's comment was probably snarky in nature, his advice is sound. Everyone who is proficient in Java knows how to find their way around the Java API and uses it as their first source the minute they have a question of similar nature to yours. Learning how to find things in the Java API is paramount. Java is blessed in having one of the best sets of API document around too. Commented Jan 15, 2011 at 13:24

4 Answers 4

2

Just don't read by newlines using readLine() method, but read char-by-char using read() method.

for (int c = 0; (c = reader.read()) > -1;) {
    System.out.print((char) c);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You asked for another class to use, so in that case give Scanner a try for this. It's usually used for delimiting input based on patterns or by the types inferred from the input (e.g. reading on a byte-by-byte bases or an int-by-int basis, or some combination thereof). However, you can use it as just a general purpose "reader" here as well, to cover your use case.

Comments

1

When you read anything from a server, you have to strictly follow the communication protocol. For example the server might be an HTTP server or an SMTP server, it may encrypt the data before sending it, some of the data may be encoded differently, and so on.

So you should basically ask: What kind of server do I want to access? How does it send the bytes to me? And has someone else already done the work of interpreting the bytes so that I can quickly get to the data that I really want?

If it is an HTTP server, you can use the code new URL("http://example.org/").openStream(). Then you will get a stream of bytes. How you convert these bytes into characters, strings and other stuff is another task.

Comments

0

You could try

InputStream is = ... // from input
String text = IOUtils.toString(is);

turns the input into text, without without newlines (it preserves original newlines as well)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.