码迷,mamicode.com
首页 > 编程语言 > 详细

java IO(BIO)、NIO、AIO

时间:2018-05-09 21:33:28      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:style   span   dex   sage   一个   end   建立连接   his   and   

IO

服务端ServerSocket 客户端Socket

缺点每次客户端建立连接都会另外启一个线程处理。读取和发送数据都是阻塞式的。

如果1000个客户端建立连接将会产生1000个线程

Server端

package bhz.bio.test;

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


public class Server {
    private int port;
    private ServerSocket serverSocket;

    public Server(int port) {
        this.port = port;

    }

    public void start() throws IOException {
        try {
            serverSocket = new ServerSocket(port);
            while (true) {
               Socket socket = serverSocket.accept();// 阻塞等待客户端建立连接
               new Thread(new ServerHandler(socket)).start();//另外起一個線程處理客戶端的請求
            }
        } catch (IOException e) {
             e.printStackTrace();
            // TODO Auto-generated catch block
            System.out.println("服务器启动失败");
        }finally {
            if(serverSocket!=null) {
                serverSocket.close();//释放资源操作
            }

} } }

ServerHandler

package bhz.bio.test;

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

public class ServerHandler implements Runnable {
    private Socket socket;

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

    @Override
    public void run() {
        // TODO Auto-generated method stub

        BufferedReader in = null;
        PrintWriter out = null;
        try {
            while (true) {
                // 监听客户端的发送消息
                out = new PrintWriter(socket.getOutputStream(),true); 
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String content = in.readLine();
                
                System.out.println("接收到客户端发送的消息:" + content);//
                out.println("哈哈哈");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket = null;
        }

    }

}

 

客户端

package bhz.bio.test;

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

public class Client {
    private String ip;
    private int port;
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    public Client(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public void connect() {
        try {
            socket = new Socket(ip, port);
            out = new PrintWriter(socket.getOutputStream(),true); 
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("建立連接失敗");
        }

    }
    
    public void send(String message) throws IOException {
        
         out.println(message);
         
         try {
             String content=in.readLine();
               System.out.println("接收到服务端的数据:"+content);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
      
         
    }
}

测试类

package bhz.bio.test;

import java.io.IOException;

public class mainTest {
  public static void main(String[] args) {
      //启动服务端
    new Thread(new Runnable() {
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Server server=new Server(8089);
            try {
                server.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }).start();

    new Thread(new Runnable() {
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Client client=new Client("127.0.0.1", 8089);
            client.connect();//与服务器建立连接
            try {
                client.send("你好呀");
                client.send("你好呀2");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();
}
}

输出

接收到客户端发送的消息:你好呀
接收到服务端的数据:哈哈哈
接收到客户端发送的消息:你好呀2
接收到服务端的数据:哈哈哈

使用线程池限制客户端数量(伪异步)

ServerHandler修改

package bhz.bio.test;

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

public class ServerHandler implements Runnable {
    private Socket socket;

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

    @Override
    public void run() {
        // TODO Auto-generated method stub

        BufferedReader in = null;
        PrintWriter out = null;
        try {
            while (true) {
                // 监听客户端的发送消息
                out = new PrintWriter(socket.getOutputStream(),true); 
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String content = in.readLine();
                
                System.out.println("接收到客户端发送的消息:" + content);//
                out.println("哈哈哈");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            socket = null;
        }

    }

}

当使用正在连接的客户端超过50个过后 阻塞

java IO(BIO)、NIO、AIO

标签:style   span   dex   sage   一个   end   建立连接   his   and   

原文地址:https://www.cnblogs.com/LQBlog/p/9016701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!