Java Socket 通信(同步阻塞式I/O)

5 篇文章 0 订阅

java实现socket通信比较简单,因为它提供了ServerSocket 和Socket类。如下为一个简单的实例:TimeServer与TimeClient

1 TimeServer

public class TimeServer {

    public static void main(String[] args) throws IOException {

       int port = 8080;//server 端口采用8080
       ServerSocket server = null;
       Socket socket = null;
       try {
           server = new ServerSocket(port);
           while(true){
               socket = server.accept();
               new Thread(new TimeHandler(socket)).start();
           }
       } catch (Exception e) {
          e.printStackTrace();
       }finally{
           if(server!=null){
               server.close();
               server =null;
               System.out.println("the time server close...");
           }
       }
    }

}

class TimeHandler implements Runnable{

    private Socket socket;

    public TimeHandler(){

    }
    public TimeHandler(Socket socket){
        this();
        this.socket = socket;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName()+" start success...");
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(),true);
            while(true){
                String str = in.readLine();
                if("end".equals(str)){
                    break;
                }
                String time = str+":"+System.currentTimeMillis();
                out.println(time);
            }
        } catch (Exception e) {
             e.printStackTrace();
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in = null;
            }
            if(out!=null){
                out.close();
                out= null;
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                socket =null;
            }
        }

    }
}

TimeServer对每一个Socket连接建立一个Handler处理线程,处理线程对inputstream流中的数据进行相应的处理后,将处理结果通过PrintWriter发送给客户端,在最后需要关闭输入流、输出流和socket套接字句柄资源。

2 TimeClient

public class TimeClient {

    public static void main(String[] args) {
        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("hello");
            out.println("world");
            out.println("end");

            System.out.println("send message success...");
            while(true){
                String res = in.readLine();
                if("".equals(res)||res==null){
                    break;
                }
                System.out.println("the res is:"+res);
            }


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

    }

}

问题分析:同步阻塞式I/O的问题在于一个新的客户端请求接入时,服务端必须创建一个新的线程来处理接入的客户端,一个线程只能处理一个客户端,这种模型往往无法满足高性能、高并发的接入场景。

以上实例改编参考于《Netty权威指南(第2版)》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值