Home > Java > javaTutorial > Example code for single-threaded network communication

Example code for single-threaded network communication

零下一度
Release: 2017-07-23 10:35:21
Original
1314 people have browsed it

The first essay since learning Java, I will write about a problem that is easy to encounter when beginners learn Socket programming. Following the example in the textbook, I typed the following single-threaded network communication code:

This paragraph The code should be a must for beginners to learn Socket programming, and the logic is relatively simple. But at the beginning, after my TCPServer class and TCPClient class were run, the information about the interaction between the two through streams could not be printed on the console. I was stuck here for a long time. I searched the reason online and basically determined that it should be an input stream or an output stream. blocked. But after trying for a long time, the problem still hasn't been solved. Later, I accidentally discovered that the instance method readLine() of the BufferedReader class returned only after reading the newline character. Only then did I discover the cause of the blocking. It turned out that the information written by my client and server to the output stream did not have a newline at all. In this way, readLine() never really returns, so both ends do not write content in the output stream, and the console naturally cannot print the content.

This problem is actually quite low-level, but there is no guarantee that beginners will not make it. Please make a brief note here to remind yourself to pay attention to details.

The above is the detailed content of Example code for single-threaded network communication. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template