BIO编程

作者: yongguang423 | 来源:发表于2018-09-17 06:45 被阅读22次

    Blocking IO: 同步阻塞的编程方式。
    BIO 编程方式通常是在JDK1.4 版本之前常用的编程方式。编程实现过程为:首先在服务
    端启动一个ServerSocket 来监听网络请求,客户端启动Socket 发起网络请求,默认情况下
    ServerSocket 回建立一个线程来处理此请求,如果服务端没有线程可用,客户端则会阻塞等
    待或遭到拒绝。
    且建立好的连接,在通讯过程中,是同步的。在并发处理效率上比较低。大致结构如下:


    BIO结构图

    同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就
    需要启动一个线程进行处理,如果这个连接不做任何事情会造成不必要的线程开销,当然可
    以通过线程池机制改善。

    BIO 方式适用于连接数目比较小且固定的架构,这种方式对服务器资源要求比较高,并发局限于应用中,JDK1.4以前的唯一选择,但程序直观简单易理解。
    使用线程池机制改善后的BIO模型图如下:


    应用线程池后的BIO
    package com.bjsxt.socket.bio.threadpool;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    import java.util.concurrent.TimeUnit;
    
    public class Client {
        public static void main(String[] args) {
            String host = null;
            int port = 0;
            if(args.length > 2){
                host = args[0];
                port = Integer.parseInt(args[1]);
            }else{
                host = "127.0.0.1";
                port = 9999;
            }
            
            Socket socket = null;
            BufferedReader reader = null;
            PrintWriter writer = null;
            Scanner s = new Scanner(System.in);
            try{
                socket = new Socket(host, port);
                String message = null;
                
                reader = new BufferedReader(
                        new InputStreamReader(socket.getInputStream(), "UTF-8"));
                writer = new PrintWriter(
                        socket.getOutputStream(), true);
                while(true){
                    message = s.nextLine();
                    if(message.equals("exit")){
                        break;
                    }
                    writer.println(message);
                    writer.flush();
                    System.out.println(reader.readLine());
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                socket = null;
                if(reader != null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                reader = null;
                if(writer != null){
                    writer.close();
                }
                writer = null;
            }
        }
    }
    
    
    package com.bjsxt.socket.bio.threadpool;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Server {
    
        public static void main(String[] args) {
            int port = genPort(args);
            
            ServerSocket server = null;
            ExecutorService service = Executors.newFixedThreadPool(50);
            
            try{
                server = new ServerSocket(port);
                System.out.println("server started!");
                while(true){
                    Socket socket = server.accept();
                    
                    service.execute(new Handler(socket));
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(server != null){
                    try {
                        server.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                server = null;
            }
        }
        
        static class Handler implements Runnable{
            Socket socket = null;
            public Handler(Socket socket){
                this.socket = socket;
            }
            @Override
            public void run() {
                BufferedReader reader = null;
                PrintWriter writer = null;
                try{
                    
                    reader = new BufferedReader(
                            new InputStreamReader(socket.getInputStream(), "UTF-8"));
                    writer = new PrintWriter(
                            new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                    String readMessage = null;
                    while(true){
                        System.out.println("server reading... ");
                        if((readMessage = reader.readLine()) == null){
                            break;
                        }
                        System.out.println(readMessage);
                        writer.println("server recive : " + readMessage);
                        writer.flush();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    if(socket != null){
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    socket = null;
                    if(reader != null){
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    reader = null;
                    if(writer != null){
                        writer.close();
                    }
                    writer = null;
                }
            }
            
        }
        
        private static int genPort(String[] args){
            if(args.length > 0){
                try{
                    return Integer.parseInt(args[0]);
                }catch(NumberFormatException e){
                    return 9999;
                }
            }else{
                return 9999;
            }
        }
        
    }
    
    
    

    相关文章

      网友评论

          本文标题:BIO编程

          本文链接:https://www.haomeiwen.com/subject/xgchnftx.html