directory
The read() method reads the input stream
The Readline() method reads the input stream
Hello! Hi, I’m Grey Ape, a bug writing programmer,
Recently, I have been doing project development, so I have little time to take care of the blog. Today, I would like to spare some time to share with you some small problems I met during the development.
When USING TCP/IP protocol for network communication, I encountered such a problem. When I used another computer as a server to communicate with the client on the same LAN, I found that C/S connection could be established between the two computers. However, for the data sent by the server, The length of the input stream of a character cannot be read().
java.net.SocketTimeoutException: Read timed out
Namely read timeout, but from the server sends the data is not very long, so according to the normally read timeout situation is generally won’t appear, after careful study found that because the read () method is read a single character, will be at the end, the end flow that is to say as long as the party has been sending duplicate data, Read () would have been reading a long input stream, which of course would have caused read timeouts, and my test was to keep the server sending the same data to the client. It is obviously not possible to read such an operation with the read() method.
In addition to the read() method, there are also readline() methods for reading input stream data. However, there are obvious differences between the two methods.
The read() method reads the input stream
The read() method reads a single character, or as long as there is an input stream, and returns the character read as an integer ranging from 0 to 65535 (0x00-0xFFFF), or -1 if the end of the stream has been reached
The read() method can be used for input streams that are sent only once, as follows:
InputStream is = client.getInputStream(); // Get the input stream from the client
byte[] b = new byte[1024]; // Define a byte array
int len = is.read(b); // Since information is transmitted in binary form, data is read in binary form
String data = new String(b, 0,len);
System.out.println("Input stream message:" + data);
Copy the code
The Readline() method reads the input stream
The readLine method reads one line of text. A line is considered terminated by one of the following characters: a newline (‘\n’), carriage return (‘\r’), or a newline immediately after carriage return. The value returned is a string containing the contents of the line, without any line terminators, or null if the end of the stream has been reached
In other words, if the readLine method reads an input stream that contains a carriage return newline character, the readLine method ends the reading. In this way, we can add a carriage return newline character at the end of each message that is sent repeatedly, so that the readLine method will end automatically when it reads this symbol.
Specific use is as follows:
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "gb2312"));
String msg = br.readLine();
if(msg ! =null) {
System.out.println("Received input stream message:" + msg + "\n");
}
Copy the code
Well, the introduction of the two methods of network communication to read the information of the input stream is first shared with you here, where there are shortcomings also hope that you are correct,
Feel good remember to like attention yo!
Big bad Wolf accompany you to progress together!