🔍
📢
package main;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        new Thread(Main::serverSocket).start();

        System.out.println("Please input:");
        try (var s = new Socket("127.0.0.1", 12345);
                var out = new PrintWriter(s.getOutputStream(), true);
                var in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                var stdIn = new BufferedReader(new InputStreamReader(System.in))) {
            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                System.out.println("Echo: " + in.readLine());
            }
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public static void serverSocket() {
        try (var ss = new ServerSocket(12345); var s = ss.accept();
                var out = new PrintWriter(s.getOutputStream(), true);
                var in = new BufferedReader(new InputStreamReader(s.getInputStream()))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                out.println(inputLine);
            }
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}