Pits encountered using the ReadLine () method

Source: Internet
Author: User
Tags readline

In the afternoon playing TCP/IP Socket communication, the use of BufferedReader readLine () encountered a pit, now finally resolved, hereby recorded.

The program is very simple, customer segment from the console read user input, and then sent to the server side, the main code is as follows


Client:

Socket s = new socket ("192.168.0.4", 20022);
 BufferedReader reader = new BufferedReader (new InputStreamReader (system.in))//user gets user input

 outputstream OS = S.getoutputstream ()///is used to output System.out.println to the server
 ("Please enter the text to send:");
 String input;
 while ((input= reader.readline ())!= null) {
 os.write (input.getbytes ("Utf-8"));
 

Server side:

Buffedreader reader = new BufferedReader (New InputStreamReader (S.getinputstream (), "Utf-8"));
String content;
while (content = Reader.readline ())!=null) {
            System.out.println ("Client data:" +content);
}


When the result is run, the server side does not read the data when the client enters the data and presses the carriage return.

It is found that the server can receive data only after the client executes the Os.write () method, plus the Os.close () method, but it closes the client's Socket at the same time, so it can only be routed once, apparently not the purpose that the program wants to achieve.

A search on the Internet, saw this article: was ReadLine () toss a, suddenly enlightened.

The original ReadLine () method returns the read result only if a carriage return (\ r) or newline character (\ n) is encountered while a row is being read, and it is important that the read content returned by ReadLine () does not contain a newline character or a carriage return character;

Also, when Realline () reads empty, it does not return null, but it blocks, and the ReadLine () method returns null only if the read input stream has been incorrectly or is closed.


So my program has a problem with the reason I found:

Because ReadLine () is used on the client to read user input, ReadLine () returns the read content when the user presses the ENTER key.

However, the returned content does not contain line breaks, and when read again with ReadLine () on the server side, the ReadLine () method blocks waiting for line breaks because the read content does not have a newline character, which is why the server side has no output.

WORKAROUND: Once the client enters a carriage return, manually add "\ n" or "\ r" to the input, and then write to the server;

while (input = Reader.readline ())!= null) {
                input = input+ "\ n";//Manually add carriage return
                os.write (input.getbytes ("Utf-8"));
            

Or read by using the Read () method on the server side.


Thank you very much Swingline's article: Click on the Open link



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.