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!!
-
2Here's the Java API do: java.sun.com/javase/6/docs/api - learn how to use it.Michael Borgwardt– Michael Borgwardt2011-01-15 12:03:46 +00:00Commented Jan 15, 2011 at 12:03
-
2Michael, 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).Joey– Joey2011-01-15 12:10:08 +00:00Commented Jan 15, 2011 at 12:10
-
2Joey: 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.whaley– whaley2011-01-15 13:24:19 +00:00Commented Jan 15, 2011 at 13:24
4 Answers
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
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.