client
package com.sun.mysocket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** * * @author JiangYuan Sun * @date 2019-04-18 11:23 */ public class Client { /** * * @author JiangYuan Sun * @throws IOException * @date 2019-04-18 11:23 */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub System.out.println("聊天通道已建立,请输入:"); Socket socket = new Socket("127.0.0.1",1000); new Thread(new Send(socket)).start();; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); try { String msg;//服务器发过来的信息 while ((msg = bufferedReader.readLine()) != null) { System.out.println("##:" + msg); } } catch (IOException e) { System.out.println("警告:断开连接!"); try { if (!socket.isClosed()) { socket.close(); } } catch (IOException e1) { System.out.println("读取线程:关闭socket出现错误"); } } System.exit(1); } } class Send implements Runnable { Socket socket=null; /** * * @author JiangYuan Sun * @date 2019-04-18 15:06 */ public Send(Socket socket) { this.socket = socket; } public void run() { //获取一个输出流 OutputStream outputStream = null; try { outputStream = socket.getOutputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //把输出流 包装成输入流 PrintWriter printWriter = new PrintWriter(outputStream); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); String msg; while ((msg = scanner.nextLine())!=null) { if(socket.isClosed()) { break; } else { if(msg.equals("88")) { System.out.println("断开和服务器的连接!"); try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } printWriter.println(msg); printWriter.flush(); } } }
server
package com.sun.mysocket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /** * * @author JiangYuan Sun * @date 2019-04-18 14:09 */ public class Server { /** * * @author JiangYuan Sun * @throws IOException * @date 2019-04-18 14:09 */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub ServerSocket serverSocket = new ServerSocket(1000); System.out.println("聊天通道已建立"); Socket socket; BufferedReader bufferedReader; while (true) { socket = serverSocket.accept(); System.out.println("一客户端连接。"); Thread writerThread = new Thread(new SeverSend(socket)); writerThread.start(); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); try { String msg;//客户端发过来的信息 while ((msg = bufferedReader.readLine()) != null) { System.out.println("##客户端:" + msg); } } catch (IOException e) { try { if (!socket.isClosed()) { socket.close(); } } catch (IOException e1) { System.out.println("关闭socket出现错误"); } } System.out.println("提示:当前客户端已经断开连接,服务器正等待下一个客户端的连接。"); } } }
package com.sun.mysocket; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** * * @author JiangYuan Sun * @date 2019-04-18 15:27 */ public class SeverSend implements Runnable { private Socket socket = null; private PrintWriter printWriter; private Scanner scanner; public SeverSend(Socket socket) throws IOException { this.socket = socket; scanner = new Scanner(System.in); printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); } @Override public void run() { String msg;//要发送的信息 try { while ((msg = scanner.nextLine()) != null) { if (msg.equals("88")) { if (!socket.isClosed()) { System.out.println("服务器手动与客户端断开"); socket.close(); } break; } printWriter.println(msg); //TODO: redis //TODO: 推送 // socket.getOutputStream().write(); } } catch (IOException io) { System.out.println("关闭socket出现问题"); } catch (Exception e) { System.out.println("异常关闭客户端(可能是直接关闭退出窗口)"); } } }
文章来源: https://blog.csdn.net/weixin_41825468/article/details/90770887