Netty学习-传统BIO

2 篇文章 0 订阅

传统的BIO通信模型的服务端,通常由一个独立的Acceptor线程负责舰艇客户端的连接,它接收到客户端连接请求后为每个客户端创建一个新的线程进行链路处理,处理完成之后,通过输出流返回应答给客户端,线程销毁。这是典型的一对一请求应答模型。

下面是一个Time的代码例子

package BIO;

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

/**
 * @author 陈晨
 * @date 2019/8/8
 * @Description 类描述
 */
public class TimeClient {
    public static void main(String[] args) throws IOException {
        int port=8080;
        Socket socket=null;
        BufferedReader in =null;
        PrintWriter out=null;
        try {
            socket = new Socket("127.0.0.1", port);
            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("TIME ORDER");
            System.out.println("Send order2server succeed");
            String resp = in.readLine();
            System.out.println(resp);
        }catch (Exception e){

        }
        finally {
            if(out!=null){
                out.close();
                out=null;
            }
            if (in!=null){
           try {
               in.close();
           }catch (IOException e){
               e.printStackTrace();
           }
           in=null;
            }
            if(socket!=null){
                try{
                    socket.close();
                }catch (IOException e)
                {
                    e.printStackTrace();
                }
                socket=null;
            }

        }
        }

}

package BIO;

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

/**
 * @author 陈晨
 * @date 2019/8/8
 * @Description 类描述
 */
//同步阻塞
public class TimeServer {
    public static void main(String[] args) throws IOException {
        int port=8080;
        ServerSocket server=null;
        try {
            server=new ServerSocket(port);
            System.out.println("port:"+8080);
            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("close");
                server.close();
                server=null;
            }
        }
    }
}

package BIO;

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

/**
 * @author 陈晨
 * @date 2019/8/8
 * @Description 类描述
 */
public class TimeServerHandler implements Runnable {
    private Socket socket;
    public TimeServerHandler(Socket socket) {
    this.socket=socket;
    }
    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(true){
                body=in.readLine();
                if(body==null) {
                    break;
                }
                    System.out.println("receive"+body);
                currentTime="QUERY TIME ORDER".equalsIgnoreCase(body)?new java.util.Date(System.currentTimeMillis())
                        .toString(): "BAD ORDER" ;
                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;
            }
        }
    }
}

BIO是一种几乎不咋用的IO通信,因为它是同步阻塞的I/O;

来自《Netty权威指南》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值