BIO编程

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请注明出处,并告知本人 https://blog.csdn.net/the_conquer_zzy/article/details/83097739

传统同步阻塞模型开发中,ServerSocket负责绑定IP,启动监听端口,意思就是说listen阶段就可以连接了

TimeServer 源码分析

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by zzy on 18-10-16.
 */
public class TimeServer {

    public static void main(String[] args){

        int port=9001;
        if(args!=null&&args.length>0){
            port=Integer.parseInt(args[0]);
        }

        ServerSocket server=null;

        try {
            server=new ServerSocket(port);

            System.out.println("The time Server is start in port: "+port);
            Socket  socket=null;
            while(true){
                socket=server.accept();

                new Thread(new TimeServerHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(server!=null){
                System.out.println("The time server close");
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                server=null;
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

/**
 * Created by zzy on 18-10-16.
 */
public class TimeServerHandler implements Runnable {

    private Socket socket;

    public TimeServerHandler(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 currentTime=null;

            String body=null;

            while((body=in.readLine())!=null){

                if(body.equalsIgnoreCase("Query Time Order")){
                    currentTime=new Date(System.currentTimeMillis()).toString();
                }else{
                      currentTime="BAD ORDER";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();

            if(in!=null){
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

            if(out!=null){
                out.close();

                out=null;
            }

            if(this.socket!=null){
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                this.socket=null;
            }
        }
    }
}

可以看到try catch finally 关闭资源比较繁琐。

利用java7开始带的try with resource statement

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

/**
 * Created by zzy on 18-10-16.
 */
public class TimeServerHandler implements Runnable {

    private Socket socket;

    public TimeServerHandler(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 currentTime=null;

            String body=null;

            while((body=in.readLine())!=null){

                if(body.equalsIgnoreCase("Query Time Order")){
                    currentTime=new Date(System.currentTimeMillis()).toString();
                }else{
                      currentTime="BAD ORDER";
                }
                System.out.println("The time server receiver is: "+body);
                out.println(currentTime);
            }
        } catch (IOException e) {
            e.printStackTrace();

            if(in!=null){
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

            if(out!=null){
                out.close();

                out=null;
            }

            if(this.socket!=null){
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                this.socket=null;
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/the_conquer_zzy/article/details/83097739
BIO