• netty学习之BIO(一)


    BioServer - 

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * Description:
     *
     * @author maduar maduar@163.com
     * @version 1.1.1 02/04/2019
     */
    public class BioServer {
    
        private static final int PORT = 8088;
    
        public static void main(String[] args) {
    
            try (ServerSocket serverSocket = new ServerSocket(PORT)) {
    
                Socket socket;
    
                while (true) {
                    socket = serverSocket.accept();
                    new Thread(new BioHandler(socket)).start();
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Date;
    
    /**
     * Description:
     *
     * @author maduar maduar@163.com
     * @version 1.1.1 02/04/2019
     */
    public class BioHandler implements Runnable {
    
        private Socket socket;
    
        public BioHandler(Socket socket) {
            this.socket = socket;
        }
    
        @Override
        public void run() {
            try (
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
                    PrintWriter printWriter = new PrintWriter(this.socket.getOutputStream(), true)) {
    
                String body;
    
                while ((body = bufferedReader.readLine()) != null && body.length() != 0) {
                    System.out.println("receive msg: " + body);
                    printWriter.println(new Date().toString());
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

      

     

    client

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    /**
     * Description:
     *
     * @author maduar maduar@163.com
     * @version 1.1.1 02/04/2019
     */
    public class Client {
    
        private static final int PORT = 8088;
        private static final String HOST = "127.0.0.1";
    
        public static void main(String[] args) {
    
            try (
                    Socket socket = new Socket(HOST, PORT);
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
    
                out.println("I am client .");
                String resp = in.readLine();
                System.out.println("current time is :" + resp);
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

      

  • 相关阅读:
    转:马云邮件全文
    XIFF资料1
    代码还是请一个字母一个字母敲(如果您只想混口饭吃就不要读了本文只面向想成为hacker的程序员)
    一个本来很有希望的项目噶然而止,脑子一下子空了
    转:进京感受一个技术人职业发展心得
    java中定义接口
    两个大数相乘(纯C实现)
    [ios学习入门1]hello,word!
    两台电脑通信的连接过程
    谁说引用不可改变
  • 原文地址:https://www.cnblogs.com/maduar/p/10646324.html
Copyright © 2020-2023  润新知