I have been learning java web before, such as front-end framework, SSM, nginx and database. My understanding of network programming can be said to be almost zero. Recently, in my work, I need to use netty to realize some functions of Intranet communication, which is more embarrassing. I instantly feel that the world is full of malice. Isn't this a dead hand under my weakness?
I found some explanations and examples of netty on the Internet. I didn't understand the theoretical part. In fact, I also wrote code with reference to the examples. This is really "network programming". I smiled.
In order to learn this knowledge blind spot well, I decided to start exploring from the socket. I simply read a few documents and found that the basic operation seems not difficult. I just wanted to start a minimalist version of terminal chat and practice. Why do I want to do terminal chat? (of course, it's because I don't want to learn the knowledge of java interface development). As a result, the speed of light hit my face. Shouldn't it? My idea is very beautiful. One client and one server have two threads. One thread is responsible for sending data and the other thread is responsible for receiving data. As for the thread receiving data, I won't do how to monitor data. Let's have a simple and simple cycle and judgment. However, it just can't communicate normally?
Finally, after consulting several documents, I found the problem. Because I know little about io streaming, I'm very embarrassed. It seems that the data hasn't passed
The code is as follows (it's easy to understand and very delicious. It's my own memo):
public class SendHandler implements Runnable { PrintWriter writer = null; public SendHandler(PrintWriter writer) { this.writer = writer; } @Override public void run() { Scanner scanner = new Scanner(System.in); while (true) { writer.println(scanner.nextLine()); } } }
These are the processors that send data
public class ReciveHandler implements Runnable { BufferedReader reader = null; public ReciveHandler(BufferedReader reader) { this.reader = reader; } @Override public void run() { while (true) { try { System.out.println("RE:" + reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } } }
The above is the processor that receives the data
public class Client { public static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String[] args) throws IOException { Socket socket = new Socket("0.0.0.0", 8089); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); send(writer); recive(reader); } public static void send(PrintWriter writer) { pool.execute(new SendHandler(writer)); } public static void recive(BufferedReader reader) { pool.execute(new ReciveHandler(reader)); } }
The above is the client
public class Server { public static ExecutorService pool = Executors.newFixedThreadPool(2); public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8089); Socket socket = serverSocket.accept(); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter writer = new PrintWriter(socket.getOutputStream(), true); send(writer); recive(reader); } public static void send(PrintWriter writer) { pool.execute(new SendHandler(writer)); } public static void recive(BufferedReader reader) { pool.execute(new ReciveHandler(reader)); } }
The above is the server
Then, there's no then
If some of my friends who have just come into contact with this aspect feel that they don't understand it (the probability should be relatively small), you can learn about thread pool, io stream and socket.
Slip away, leave like a rookie