传统的BIO编程

在传统的同步阻塞模型开发中,ServerSocket绑定监听端口,Socket发起连接操作。连接成功后,双方通过输入输出流进行同步阻塞式通信。

下面以时间服务器为例说明。
客户端向服务端发送“Server Time”,服务端向客户端返回服务端当前时间。

服务端代码:

/**
 * @author Donny
 * Created by Donny on 2017/11/11.
 */
public class TimeServer {
    public static void main(String[] args) {
        TimeServer timeServer = new TimeServer();
        timeServer.start(8080);
    }
    private void start(int port) {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(port);
            System.out.println("TimeServer is running...");
            while (true) {
                Socket socket = ss.accept();
                new Thread(new TimeServerHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                es.shutdownNow();
            }
        }
    }
    class TimeServerHandler implements Runnable {
        private Socket socket = null;
        public TimeServerHandler(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            BufferedReader br = null;
            PrintWriter pw = null;
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
                while (true) {
                    String input = br.readLine();
                    if (null == input) {
                        break;
                    }
                    System.out.println("接收到客户端请求:" + input);
                    if ("Server Time".equalsIgnoreCase(input)) {
                        pw.println((new Date()).toString());
                    }
                }
            } catch (IOException e) {
                System.out.println("ip:" + socket.getInetAddress().getHostAddress() + " I/O异常");
            }
            finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (pw != null) {
                    pw.close();
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

客户端代码:

/**
 * @author Donny
 * Created by Donny on 2017/11/11.
 */
public class TimeClient {
    public static void main(String[] args) {
        TimeClient tc = new TimeClient();
        tc.connect("127.0.0.1",8080);
    }
    private void connect(String host,int port) {
        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            socket = new Socket(host,port);
            in = socket.getInputStream();
            out = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out),true);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            System.out.println("请求服务器时间");
            pw.println("Server Time");
            String response = br.readLine();
            System.out.println("response:" + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

由一个Acceptor线程负责监听客户端连接请求,接收到客户端连接请求后,为每个客户端分配一个新的线程进行链路处理。处理完成后,通过输出流应答客户端。

BIO模型的最大问题在于缺乏弹性伸缩能力,因为服务端线程和客户端是呈现的1:1的关系。当线程数膨胀后,系统性能急剧下降,随着并发访问量的继续增大,系统会发生线程堆栈溢出、创建新线程失败等问题,最终导致进程宕机,不能对外提供服务。

参考《Netty权威指南》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值