Simple practice of java Socekt

1 foreword

In java, network communication is generally socket based. Here is a simple example of socket communication

2 socket communication practice

Let's see the server first

/**
 * @author Created by qiyei2015 on 2018/1/5.
 * @version: 1.0
 * @email: 1273482124@qq.com
 * @description:
 */
public class SocketServer {

    public static void main(String[] args){
        new SocketServer().start();
    }

    public void start(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(8888);
            while (true){
                socket = serverSocket.accept();
                newSocketRun(socket);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * Open a new socket to run
     * @param socket
     */
    public void newSocketRun(final Socket socket){
        new Thread(new Runnable() {
            public void run() {
                BufferedReader inputReader = null;
                BufferedWriter outputWirter = null;
                System.out.println("client:" + socket.hashCode() + " connected ! ");
                try {
                    inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    outputWirter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    String s = null;
                    while (!(s = inputReader.readLine()).equals("bye")){
                        System.out.println("receive:" + s);
                        outputWirter.write("server reply :" + s + "\n");
                        outputWirter.flush();
                        System.out.println("send:" + s);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        inputReader.close();
                        outputWirter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

It can be seen that the server side mainly creates a ServerSocket, and sets the port number, then calls serverSocket.accept(); waiting for the connection of the client; if there is no connection, it will block in here, after the connection is successful, it will return a Socket object and then operate the Socket flow.
In order to support multiple client connections, a thread is created to handle each successful client connection

Let's look at the client code

/**
 * @author Created by qiyei2015 on 2018/1/5.
 * @version: 1.0
 * @email: 1273482124@qq.com
 * @description:
 */
public class SocketClient {

    public static void main(String[] args){
        new SocketClient().start();
    }

    public void start(){
        BufferedReader inputReader = null;
        BufferedReader bufferedReader = null;
        BufferedWriter outputWriter = null;

        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1",8888);
            outputWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            inputReader = new BufferedReader(new InputStreamReader(System.in));
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = null;
            while (!(s = inputReader.readLine()).equals("bye")){
                //End required \ n
                outputWriter.write(s + "\n");
                outputWriter.flush();
                System.out.println("receive server:" + bufferedReader.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputReader.close();
                bufferedReader.close();
                outputWriter.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

The client's code is also relatively simple, mainly to create the Socket object, and specify the ip and port number
Then, just like the client, the data can be sent and received by operating the flow of socket object

The operation effect is as follows:
Server:

client:

The program is very simple, and it won't be posted

Tags: socket Java network

Posted on Tue, 05 May 2020 00:19:46 -0700 by Pilly