BIOベースのTCPプログラミングの簡易実装

6650 ワード

サービス側


一定の同時量の保証、および機器が高すぎる同時麻痺にならないように、スレッドプールを用いて最大スレッド量を制御する方式でメンテナンスするがBIOの方法はブロックされており、実際の開発ではほとんど使用されずjava 1を採用している.7後のNIO 2.バージョン0
public class BIOBootstrap {
    public static void main(String[] args) throws Exception {
        start4();
    }

    @SuppressWarnings("deprecation")
    private static void start4() throws Exception {

        //1、 ServerSocket
        ServerSocket servletSocket=new ServerSocket();
        //2、  
        servletSocket.bind(new InetSocketAddress(9999));

        ExecutorService executorService=Executors.newFixedThreadPool(150);


        while (true) {
            //3、  block
            System.out.println(" 9999 ");
            final Socket socket = servletSocket.accept();
            executorService.submit(new Runnable() {

                public void run() {
                    // 
                    //1、 
                    try {
                        InputStream is = socket.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is,
                                Charset.forName("utf-8"));
                        BufferedReader br = new BufferedReader(isr);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = br.readLine()) != null) {
                            sb.append(line);
                        }
                        System.out.println(" , ... (" + sb.toString()
                                + ")... ... .. ?");
                        //2、 
                        OutputStream os = socket.getOutputStream();
                        PrintWriter pw = new PrintWriter(os);
                        pw.println("time:" + new Date().toLocaleString());
                        pw.flush();
                        socket.shutdownOutput();
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }

    }

クライアント

public class Bootstrap {
    public static void main(String[] args) throws Exception {
        for(int i=0;i<16;i++){
            new Thread(){
                public void run() {
                    String re;
                    try {
                        re = sendMessage(" msi... ... .. ?");
                        System.out.println(re);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                };
            }.start();
        }
    }

private static String sendMessage(String msg) throws Exception {
        //1、 socket
        Socket socket=new Socket();
        //2、 
        socket.connect(new InetSocketAddress("127.0.0.1", 9999));
        // 
        //1、 
        OutputStream os= socket.getOutputStream();
        PrintWriter pw=new PrintWriter(os);
        pw.println(msg);
        pw.flush();
        socket.shutdownOutput();
        //2、 
        InputStream is = socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is, Charset.forName("utf-8"));
        BufferedReader br=new BufferedReader(isr);

        StringBuilder sb=new StringBuilder();
        String line=null;
        while((line=br.readLine())!=null){
            sb.append(line);
        }

        return sb.toString();   
    }
}