Java multi-threading in the example of ServerSocket and Socket

Start ...
1, the server:

public class ServerSocketApp {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            System.out.println("服务已启动");
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("客户" + socket.getInetAddress().getHostAddress() + "来访,");
                new Thread(() -> {
                    try {
                        InputStream inputStream = socket.getInputStream();
                        PrintStream printStream = new PrintStream(socket.getOutputStream());
                        Scanner in = new Scanner(inputStream);
                        in.useDelimiter("\n");
                        while (in.hasNext()) {
                            String message = in.next().trim();
                            if (message.equalsIgnoreCase("bye")) {
                                String bye = new String("客户端已退出,拜拜".getBytes("UTF-8"));
                                System.out.println(bye);
                                printStream.println(bye);
                            } else {
                                printStream.println("ECHO>" + message);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2, the client program:

public class ClientSocketApp {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 9999);
        Scanner in = new Scanner(System.in);
        Scanner inputStream = new Scanner(socket.getInputStream());
        PrintStream printStream = new PrintStream(socket.getOutputStream());
        in.useDelimiter("\n");
        inputStream.useDelimiter("\n");
        boolean b = true;
        System.out.println("请输入:");
        while (b) {
            if (in.hasNext()) {
                String message = in.next().trim();
                printStream.println(message);
                if (inputStream.hasNext()) {
                    String serverMessage = new String(inputStream.next().trim().getBytes("UTF-8"));
                    System.out.println(serverMessage);
                }
                if (message.equalsIgnoreCase("bye")) {
                    b = false;
                }
            }
        }
        socket.close();
    }
}

3, run
server
cd com / kyy / bases / socket ServerSocketApp.java run javac
cd java ServerSocketApp the CLASSPATH root directory of the
server initiates a
client
cd com / kyy / bases / socket ClientSocketApp.java run javac
cd root directory of the CLASSPATH java ClientSocketApp

Problems encountered:
if not through eclipse, Idea and other tools, there will be an exception "Can not find or load the main class com.kyy.bases.socket.ClientSocketApp" the
solution set classpath = .; "JAVA_HOME" / lib
or temporarily disposed in cmd window set classpath = .;

...End

Guess you like

Origin blog.51cto.com/11147669/2409029