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

来源:互联网 发布:帝国cms电影源码 编辑:程序博客网 时间:2024/06/08 06:58

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版)》

0 0