java IO(BIO)NIOAIO
Posted 意犹未尽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java IO(BIO)NIOAIO相关的知识,希望对你有一定的参考价值。
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)NIOAIO的主要内容,如果未能解决你的问题,请参考以下文章