#java#bio#简单实现的echo server

/**
 * @Author lyr
 * @create 2020/8/7 1:07
 */
public interface EchoServer {
    /**
     * 开启 port 端口的服务
     * @param port
     */
    void startServer(int port) throws IOException;
}

public interface EchoClient {
    void connect(String ipAddr,int port);
    // void send(String requestBody);
    // void quit();

}

public class EchoClientIml implements EchoClient {


    @Override
    public void connect(String ipAddr, int port) {
        try {
            new Client().connect(ipAddr,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class Client {
        void connect(String ip,int port) throws IOException {
            Socket cli = new Socket(ip, port);
            BufferedReader in = new BufferedReader(new InputStreamReader(cli.getInputStream()));
            // Writer out;
            PrintWriter out = new PrintWriter(cli.getOutputStream(),true);
            Scanner sc = new Scanner(System.in);
            while (sc.hasNextLine()) {
                String me = sc.nextLine();
                if(me.equalsIgnoreCase("quit")) {
                    out.println(me);
                    System.out.println("连接关闭");
                    break;

                }
                out.println(me);
                String resp = in.readLine();
                System.out.println(String.format("收到回复 [%s]",resp));
            }

        }
    }



    public static void main(String[] args) {

        new EchoClientIml().connect("127.0.0.1",8888);
    }

}




public class EchoServerImpl implements EchoServer {
    public static void main(String[] args) throws InterruptedException, IOException {
        EchoServer server = new EchoServerImpl();
        server.startServer(8888);
        // System.out.println("结束");
    }

    @Override
    public void startServer(int port) throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        AtomicInteger num = new AtomicInteger(0);
        while (true) {
            Socket cli = serverSocket.accept();
            new Thread(() -> {
                new Server(port, cli, num.incrementAndGet()).start();
            }).start();
        }

    }

    private static class Server {
        int port;
        int id;
        Socket cli;
        PrintWriter out;

        BufferedReader in;

        public Server(int port, Socket cli, int id) {
            this.port = port;
            this.cli = cli;
            this.id = id;


        }


        void start() {
            try {
                out = new PrintWriter(cli.getOutputStream(), true);
                in = new BufferedReader(new UTF8Reader(cli.getInputStream()));
                String body = null;
                while ((body = in.readLine()) != null) {
                    if("quit".equalsIgnoreCase(body)) {
                        in.close();
                        out.close();
                        cli.close();
                        break;
                    }
                    System.out.println(String.format("来自用户的数据[%s] %s",id, body));
                    out.println(body);

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


}

改进解决方法:

   @Override
    public void startServer(int port) throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        AtomicInteger num = new AtomicInteger(0);
        Executor executor = Executors.newFixedThreadPool(6);
        while (true) {
            Socket cli = serverSocket.accept();
            // new Thread(() -> {
            //     new Server(port, cli, num.incrementAndGet()).start();
            // }).start();
            executor.execute(() -> {
                new Server(port,cli,num.incrementAndGet()).start();
            });
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值