In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the process of Socket using readLine () method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The program is very simple. The client segment reads the user input from the console and sends it to the server. 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 (); / / for output to the server
System.out.println ("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 running, after the client enters the data and presses enter, the server does not read the data.
It is found that the server can receive the data only after the client executes the os.write () method and adds the os.close () method, but this shuts down the client's Socket at the same time, so it can only be sent once, which is obviously not what the program wants to achieve.
After searching on the Internet, I saw this article: I was tossed about by readLine (), and suddenly became enlightened.
It turns out that when the readLine () method reads a line, it will return the read result only if it encounters carriage return (\ r) or newline character (\ n). This is the meaning of "reading a line". The important thing is that the reading returned by readLine () does not contain a newline character or carriage return character.
Also, when what realLine () reads is empty, it does not return null, but blocks all the time, and the readLine () method returns null only if there is an error in the read input stream or is closed.
So I found the reason for the problem with my program:
Because readLine () is used on the client side to read user input, when the user presses enter, readLine () returns the read.
However, the content returned at this time does not contain a newline character, and when you read it again with readLine () on the server side, the readLine () method will always block waiting for a newline character because there is no newline character on the read, which is why there is no output on the server side.
Solution: after each enter by the client, manually add "\ n" or "\ r" to the input, and then write to the server
While ((input = reader.readLine ())! = null) {
Input = input+ "\ n"; / / manually add enter
Os.write (input.getBytes ("utf-8"))
}
Or use the read () method to read on the server side.
That's all for "the process of Socket using the readLine () method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.