从BIO到Netty的演变 (2)

所以这里再給一个,可以进行通信的版本。简单的业务场景可以直接修改,应用。

BIO2_Client package self; import java.io.*; import java.net.*; /** * @Description: * @Author: jarry */ public class Client { public static void main(String[] args) throws IOException { Socket socket = new Socket(); socket.setSoTimeout(2000); socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),2000); System.out.println("client startup"); dealMsg(socket); socket.close(); } private static void dealMsg(Socket clientSocket) throws IOException { // 1.获取键盘输入流 InputStream systemInputStream = System.in; // 2.将systemInputStream转化为具有缓存功能的字符输入流BufferedReader BufferedReader systemBufferedReader = new BufferedReader(new InputStreamReader(systemInputStream)); // 3.获取Socket输入流 InputStream socketInputStream = clientSocket.getInputStream(); // 4.将socketInputStream转换为具有缓存能力的字符输入流 BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(socketInputStream)); // 5.获取Socket输出流 OutputStream socketOutputStream = clientSocket.getOutputStream(); // 6.将socketOutputStream转换为打印流(用于发送String) PrintStream socketPrintStream = new PrintStream(socketOutputStream); // 用于确立连接状态的标识符 boolean flag = true; // 7.利用循环,client与server进行交互 do { // 从键盘等系统输入流获取输入字符串 String str = systemBufferedReader.readLine(); // 将str写入到socketClient的打印流(本质是输出流)。socketClient的输出流是连接Server的,用于向Server发送数据的 socketPrintStream.println(str); // 从Server获得回写(Server的回写,一定会发送到socketClient的输入流中(输入的“入”是指入socketClient) String echo = socketBufferedReader.readLine(); // 建立一个用于关闭的方式 if ("bye".equalsIgnoreCase(echo)){ flag = false; }else{ // 在控制台打印server的echo System.out.println("server echo:"+echo); } }while (flag); // 8.退出交互,需要关闭与Server连接的两个资源(输入与输出) 考虑一下lombok的@Cleanup socketBufferedReader.close(); socketPrintStream.close(); } } BIO2_Server package self; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * @Description: * @Author: jarry */ public class Server { public static void main(String[] args) throws IOException { // 建立Server的Socket,服务端不需要设置IP,以及Port // IP采用本地IP ServerSocket serverSocket = new ServerSocket(2000); System.out.println("server startup"); // 通过循环,对client的请求进行监听 while (true){ // 获得client的请求 Socket clientRequest = serverSocket.accept(); // 异步处理client的请求 ClientHandler clientHandler = new ClientHandler(clientRequest); clientHandler.start(); } } private static class ClientHandler extends Thread { Socket socketClient = null; boolean flag = true; ClientHandler(Socket socketClient){ this.socketClient = socketClient; } @Override public void run() { super.run(); // 构建系统输入流 InputStream systemInputStream = System.in; // 将系统输入流转换为字符输入流 BufferedReader systemBufferedReader = new BufferedReader(new InputStreamReader(systemInputStream)); try { // 构建socketClient的输入流(即客户端中,写入client输出流的数据) InputStream clientInputStream = socketClient.getInputStream(); // 将client的输入流转为具有存储能力的BufferedReader BufferedReader clientBufferedReader = new BufferedReader(new InputStreamReader(clientInputStream)); // 构建socketClient的输出流(用于发送数据,即客户端中,从client输入流读取的数据) OutputStream clientOutputStream = socketClient.getOutputStream(); // 将client的输出流转换为打印流,便于输出数据 PrintStream clientPrintStream = new PrintStream(clientOutputStream); // 通过循环,与客户端进行交互 do { // 读取从客户端发送来的数据,即读取socketClient的输入流转化的BufferedReader String str = clientBufferedReader.readLine(); if ("bye".equalsIgnoreCase(str)){ flag = false; clientPrintStream.println("connect interrupt"); }else{ System.out.println(str); // 发送回写数据,即将回写数据写入socketClient的输出流(客户端的输入流会获取相关数据) clientPrintStream.println(str.length()); } // 从系统输入中获取想要发送的数据 String servStr = systemBufferedReader.readLine(); // 发送到客户端 clientPrintStream.println(servStr); }while (flag); // 同样的,关闭连接资源 clientBufferedReader.close(); clientPrintStream.close(); } catch (IOException e) { e.printStackTrace(); }finally { // 无论发生什么,最后都要关闭socket连接 try { socketClient.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpfgfx.html