古老的BIO---同步阻塞模式(1)

引言:

IO通信中何为阻塞、非阻塞,同步、异步?


阻塞:应用程序获取网络数据,需等待网络传输。


非阻塞:直接获得已准备就绪的数据,无需等待。


同步和异步分别指的是应用程序层面和操作系统层面对数据读取的方式。同步:应用程序直接参与数据的读写,阻塞到某个方法,直到数据准备就绪或轮训的方式检查数据准备状态,准备就绪则获取数据。异步:操作系统层面参与数据读写,操作系统完成读写,通知应用程序。


BIO:同步阻塞模式


NIO:同步非阻塞模式


AIO(NIO2):异步非阻塞模式


可能我写的这些比较抽象,有些人暂时无法体会,后面我会一步步介绍,大家运行相应程序测试,慢慢体会里面的概念和思想。



创建Server循环监听,不断有Client连接进来,Server新创建线程去执行任务。
但此方式有缺点,Server端循环监听,没有采用线程池策略,没有考虑到服务器压力的问题,第二篇中会引用到线程池,同时会介绍同步、异步,阻塞、非阻塞的概念。

//
Server端,循环监听 public class Server { final static int PROT = 8765; public static void main(String[] args) { try { ServerSocket server = new ServerSocket(PROT); System.out.println(" server start .. "); while (true) { // 进行阻塞 Socket socket = server.accept(); // 新建一个线程执行客户端的任务 new Thread(new ServerHandler(socket)).start(); } } catch (Exception e) { e.printStackTrace(); } // finally { // if(server != null){ // try { // server.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // server = null; // } } }
//客户端
public class Client {

    final static String ADDRESS = "127.0.0.1";
    final static int PORT = 8765;
    
    public static void main(String[] args) {
        
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        
        try {
            //创建一个socket
            socket = new Socket(ADDRESS, PORT);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //拿到一个往服务端写的输出流,服务端通过输入流接受
            out = new PrintWriter(socket.getOutputStream(), true);
            
            //向服务器端发送数据
            out.println("接收到客户端的请求数据...");
            String response = in.readLine();
            System.out.println("Client: " + response);
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket = null;
        }
    }
}
public class ServerHandler implements Runnable{

    private Socket socket ;
    
    public ServerHandler(Socket socket){
        this.socket = socket;
    }
    //服务端创建的线程去执行的任务
    @Override
    public void run() {
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            out = new PrintWriter(this.socket.getOutputStream(), true);
            String body = null;
            while(true){
                body = in.readLine();
                if(body == null) break;
                System.out.println("Server :" + body);
                out.println("服务器端回送响的应数据.");
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket = null;
        }
        
        
    }

}

 

posted on 2017-04-23 23:19  老曹123  阅读(870)  评论(0)    收藏  举报

导航