微前端架构如何改变企业的开发模式与效率提升
550
2022-11-05
TCP一对多聊天服务器的实现(多线程场景)
1.服务器的连接设计
2.服务器的线程设计
3.创建服务端
package com.yqq.app3;/** * @Author yqq * @Date 2021/11/08 22:28 * @Version 1.0 */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java-.ServerSocket;import java-.Socket;/** * 接受客户端发送消息的线程 */class ChatReceive extends Thread{ private Socket socket; public ChatReceive(Socket socket){ this.socket=socket; } @Override public void run() { this.receiveMsg(); } /** * 实现接受客户端发送的消息 */ private void receiveMsg(){ BufferedReader br =null; try { br=new BufferedReader(new InputStreamReader(this.socket.getInputStream())); while(true){ String msg = br.readLine(); //将数据加入数据公共区域 synchronized ("yqq"){ ChatRoomServer.buf="["+this.socket.getInetAddress()+"]"+msg; //唤醒等待的线程,被相同锁对象使用者 "yqq".notifyAll(); } } }catch (Exception e){ e.printStackTrace(); }finally { if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(this.socket!=null){ try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}/** * 向客户端发送消息的线程 */class ChatSend extends Thread{ Socket socket=null; public ChatSend(Socket socket){ this.socket=socket; } @Override public void run() { this.sendMsg(); } /** * 将公共数据区域的数据发送给客户端 */ private void sendMsg(){ PrintWriter pw =null; try { pw =new PrintWriter(this.socket.getOutputStream()); while (true){ synchronized ("yqq"){ //让发送消息的线程处于等待状态 "yqq".wait(); //将公共数据区中的消息发送给客户端 pw.println(ChatRoomServer.buf); pw.flush(); } } }catch (Exception e){ e.printStackTrace(); }finally { if(pw!=null){ pw.close(); } if(this.socket!=null){ try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}public class ChatRoomServer { //定义公共数据区 public static String buf; public static void main(String[] args) { System.out.println("Chat Server Version 1.0"); System.out.println("Listen at 8888"); ServerSocket serverSocket =null; try { serverSocket=new ServerSocket(8888); while (true){ Socket socket=serverSocket.accept(); System.out.println("连接到:"+socket.getInetAddress()); new ChatReceive(socket).start(); new ChatSend(socket).start(); } }catch (Exception e){ e.printStackTrace(); }finally { if(serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
4.创建客户端
package com.yqq.app3;/** * @Author yqq * @Date 2021/11/08 22:28 * @Version 1.0 */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java-.ServerSocket;import java-.Socket;import java.util.Scanner;/** * 发送消息线程 */class Send1 extends Thread{ private Socket socket; private Scanner scanner; public Send1(Socket socket,Scanner scanner){ this.socket = socket; this.scanner = scanner; } @Override public void run() { this.sendMsg(); } /** * 发送消息 */ private void sendMsg(){ PrintWriter pw = null; try{ //创建向对方输出消息的流对象 pw = new PrintWriter(this.socket.getOutputStream()); while(true){ String msg = scanner.nextLine(); pw.println(msg); pw.flush(); } }catch(Exception e){ e.printStackTrace(); }finally { if(scanner != null ){ scanner.close(); } if(pw != null){ pw.close(); } if(this.socket != null){ try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}/** * 接收消息的线程 */class Receive1 extends Thread{ private Socket socket; public Receive1(Socket socket){ this.socket = socket; } @Override public void run() { this.receiveMsg(); } /** * 用于接收对方消息的方法 */ private void receiveMsg(){ BufferedReader br = null; try{ //创建用于接收对方发送消息的流对象 br = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); while(true){ String msg = br.readLine(); System.out.println("他说:"+msg); } }catch(Exception e){ e.printStackTrace(); }finally{ if(br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(this.socket != null){ try { this.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}public class GoodTCP { public static void main(String[] args) { Scanner scanner = null; ServerSocket serverSocket = null; Socket socket = null; try{ scanner = new Scanner(System.in); System.out.println("请输入:server,
5.通信
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~