最近在学习Netty,算是目前开发中主流的通信框架了。说到网络通信,不管是netty、mq等优秀的架构都脱离不开socket。
所以说,再牛逼的技术也是基础的变形,而万变不离其宗,基础很重要!基础很重要!基础很重要!
闲言少叙,直接上代码。
(以下代码需注意的点:
1、有人说socket代码需要先写后读,其实不然。之所以会这样,是因为socket在用输入流读取数据时,若没有读取到数据,则线程处于阻塞状态,个人理解成监听。为此,我用一个线程来开启输入流。
2、消息写入,使用BufferedWriter.write(data)时,由于没有把光标指向下一行,所以输入流是获取不到输入数据的,加入BufferedWriter.newLine()即可。
)
socket服务端:
package com.example.socket; import java.io.*; import java.net.Socket; import java.util.Scanner; import java.util.concurrent.Executors; @SuppressWarnings("all") public class ServerSocket implements Runnable{ public static void main(String[] args) throws IOException { int port = 8080; java.net.ServerSocket server = new java.net.ServerSocket(port); System.out.println("ServerSocket 启动,端口:" + port); Socket socket = null; while(true) { socket = server.accept(); new Thread(new ServerSocket(socket)).start(); } } private Socket server; private BufferedReader br; private BufferedWriter bw; public ServerSocket(Socket server) { this.server = server; } public void run() { try { readStart(); writeStart(); } catch (IOException e) { e.printStackTrace(); }finally { try { bw.close(); br.close(); server.close(); } catch (IOException e) { e.printStackTrace(); } } } private void readStart(){ Executors.newSingleThreadExecutor().submit(new Runnable() { public void run() { try { read(); } catch (IOException e) { e.printStackTrace(); } } }); } private void writeStart() throws IOException { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()){ write(scanner.nextLine()); } } private void write(String msg) throws IOException { System.out.println("服务端开始写入..."); bw = new BufferedWriter(new OutputStreamWriter(server.getOutputStream())); bw.write(msg); bw.newLine(); bw.flush(); } private void read() throws IOException { System.out.println("服务端开始读取,未读到数据则处于阻塞状态..."); br = new BufferedReader(new InputStreamReader(server.getInputStream())); String s = null; while ((s = br.readLine()) != null) { System.out.println("服务端接收到:" + s); } } }
socket客户端:
package com.example.socket; import java.io.*; import java.net.Socket; import java.util.Scanner; import java.util.concurrent.Executors; @SuppressWarnings("all") public class ClientSocket implements Runnable{ public static void main(String[] args) throws IOException { String ip = "127.0.0.1"; int port = 8080; Socket client = new Socket(ip, port); new Thread(new ClientSocket(client)).start(); } private Socket client; private BufferedReader br; private BufferedWriter bw; public ClientSocket(Socket client) { this.client = client; } public void run() { try { readStart(); writeStart(); } catch (IOException e) { e.printStackTrace(); }finally { try { bw.close(); br.close(); client.close(); } catch (IOException e) { e.printStackTrace(); } } } private void readStart(){ Executors.newSingleThreadExecutor().submit(new Runnable() { public void run() { try { read(); } catch (IOException e) { e.printStackTrace(); } } }); } private void writeStart() throws IOException { Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()){ write(scanner.nextLine()); } } private void write(String msg) throws IOException { System.out.println("客户端开始写入..."); bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); bw.write(msg); bw.newLine(); bw.flush(); } private void read() throws IOException { System.out.println("客户端开始读取,未读到数据则处于阻塞状态......"); br = new BufferedReader(new InputStreamReader(client.getInputStream())); String s = null; while ((s = br.readLine()) != null) { System.out.println("客户端接收到:" + s); } } }