The interface is as follows:
----------
Project directory structure
Code :
https://gitee.com/kunlunsoft/inetAdress_socket.git
d-code core notes:
1. The monitoring of the client socket by the server socket should be placed in the thread, otherwise the interface will be blocked and "crash";
new Thread(new Runnable() { @Override public void run() { try { client = server.accept(); System.out.println("There is a successful client connection:" + client.getLocalAddress().getHostAddress()); ServerApp.isConnected = true; reader = new BufferedReader(new InputStreamReader( client.getInputStream())); out = new PrintStream(client.getOutputStream()); out.println(Constant2.CRLF_newLine_placeholder + "connect successfully"); } catch (IOException e) { e.printStackTrace (); System.out.println("error"); return; } } }).start();
2. Use double loops when reading chat information through socket
The first (outermost) loop is an infinite loop, the purpose is to continuously monitor the socket input stream to determine whether there is chat information;
The second layer of loop is used to read socket, if there is nothing to read, it will exit the loop.
3. If the content is not read for two consecutive cycles, it means that the socket connection is abnormally disconnected.
Code :
https://gitee.com/kunlunsoft/inetAdress_socket.git
Simple chat tool for netty version :
rely:
<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.21.Final</version> </dependency>