Simple chat tool demo implemented by java socket

This is a simple chat tool demo implemented using native java socket, the interface uses java swing

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>

 http://i.yhskyc.com/product/1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326199555&siteId=291194637