实现功能

多好友间正常聊天,登录退出提示**
如图

此处涉及到之前的容器
点击跳转
只写了一部分以后再更容器

关闭流工具类

package 网络编程_手写聊天室_群聊过渡板;public class MultiClient {public static void main(String[] args) throws UnknownHostException, IOException {BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//获取用户名System.out.println("请输入用户名: ");String name=str.readLine();Socket  client = new Socket("localhost",8888);new Thread(new Sent(client,name)).start();new Thread(new Receive(client)).start();}
}

客户端接收类

package 网络编程_手写聊天室_群聊过渡板;public class Receive implements Runnable{private Socket client;private boolean isRunning;private DataInputStream in;public Receive(Socket client) {this.client=client; try {in=new DataInputStream(client.getInputStream());} catch (IOException e) {System.out.println("接收时出错!");release();}}@Overridepublic void run() {isRunning =true;while(isRunning) {String str="";try {str = in.readUTF();} catch (IOException e) {System.out.println("读取时出错");}if(str!=null) {System.out.println(str);}}this.release();}public  void release() {CloseUtlis utlis =new CloseUtlis();utlis.close(in);isRunning=false;}}

客户端发送类

package 网络编程_手写聊天室_群聊过渡板;public class Sent implements Runnable{private Socket client;private BufferedReader console;private DataOutputStream out;private boolean isRunning;public Sent(Socket client,String name) {this.client=client;console=new BufferedReader(new InputStreamReader(System.in));try {out =new DataOutputStream(client.getOutputStream());sent(name);//将名称发送到服务器} catch (IOException e) {System.out.println("发送数据时出错");}}@Overridepublic void run() {isRunning =true;while(isRunning) {String str="";try {str = console.readLine();} catch (IOException e) {System.out.println("读取时出错");release();}if(str!=null) {sent(str);}    }release();}public void sent(String mess) {try {out.writeUTF(mess);out.flush();} catch (IOException e) {System.out.println("发送时失败!");release();}}public  void release() {CloseUtlis utlis =new CloseUtlis();utlis.close(console,out);isRunning=false;}}

客户端

package 网络编程_手写聊天室_群聊过渡板;public class MultiClient {public static void main(String[] args) throws UnknownHostException, IOException {BufferedReader str=new BufferedReader(new InputStreamReader(System.in));//获取用户名System.out.println("请输入用户名: ");String name=str.readLine();Socket  client = new Socket("localhost",8888);new Thread(new Sent(client,name)).start();new Thread(new Receive(client)).start();}
}

服务器

package 网络编程_手写聊天室_群聊过渡板;public class MultiServer {private static CopyOnWriteArrayList<channel> all =new CopyOnWriteArrayList<MultiServer.channel>();//channel类型的容器public static void main(String[] args) throws IOException {System.out.println("服务器开始工作");ServerSocket server=new ServerSocket(8888);boolean isRunning =true;while(isRunning) {Socket client = server.accept();channel c=new channel(client);all.add(c);//管理每一个成员new Thread(c).start();}}static class channel implements Runnable{//与服务器连接的客户端private String name;private  Socket client;private DataInputStream in;private DataOutputStream out;private int port;private boolean iSsys;//是否为官方消息private boolean isRunning;channel(Socket client) throws IOException{this.client=client;out =new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));in =new DataInputStream(client.getInputStream());String name=receive();this.name=name;sent("欢迎来到聊天室");sentothers(name+"加入了聊天室", true);}@Overridepublic void run() {try {System.out.println("建立了一个连接");while(true) { String mes=receive();//输入   sentothers(mes,false);//向所有用户输出}} catch (IOException e) {System.out.println("多线程中出错");}   }public String receive() throws IOException {String mes="";mes=in.readUTF();return mes;}public void sentothers(String mes,boolean iSsys) throws IOException {for(channel other:all) {if(other==this) {continue;}if(!iSsys) {other.sent(this.name+"对全体成员说: "+mes);}else {other.sent(mes);}}}public void sent(String mes) throws IOException {out.writeUTF(mes);out.flush();}public  void release() {//释放资源并退出聊天室isRunning=false;CloseUtlis utlis =new CloseUtlis();utlis.close(in,client);all.remove(this);try {sentothers(this.name+" 离开了群聊",true );} catch (IOException e) {System.out.println("释放时出错");}}}
}

效果

网络编程_手写聊天室_群聊过渡板相关推荐

  1. springboot+websocket构建在线聊天室(群聊+单聊)

    系列导读: 1.springboot+websocket构建在线聊天室(群聊+单聊) 2.Spring Boot WebSocket:单聊(实现思路) 3.Websocket Stomp+Rabbit ...

  2. 06 java GUI 网络编程:图形界面聊天室

    仿照第4篇笔记的形式,笔者决定将GUI和网络编程部分用综合练习的方式来总结.练习项目是有图形界面的聊天室,用到了GUI中的javax.swing包和网络编程中的TCP/socket编程.GUI部分的难 ...

  3. 网络编程基础_5.3聊天室-多人聊天室

    聊天室-多人聊天室 #include <stdio.h>// 1. 包含必要的头文件和库, 必须位于 windows之前 #include <WinSock2.h> #prag ...

  4. 网络编程项目—— 多人聊天室->双人聊天

    一.Coding前的思考和步骤梳理 1. 又开始思考这个问题,拿到一个需求,是调用java底层的代码写1000行实现,还是直接调第三方库写50行实现? 都一样. 发现第一次写shell的时候其实思考过 ...

  5. Java用TCP手写聊天室 可以 私聊版加群聊版

    一:引言 想要私聊必须有规定的格式:@名字:要说的话 二:上码 1.服务端 package com.wyj.talkhome; /** * 实现一个用户可以接发多条消息 * * */ import j ...

  6. linux网络编程TCP多人聊天室

    用socket实现一个多人聊天室的思路很简单,即在服务器端定义一个fd的int型数组,用来存储已经连接的客户端的socket连接套接字fd(因为发送和接收数据都只需要借助连接套接字fd),当服务器接收 ...

  7. Go实现简易聊天室(群聊)

    参考:Go 群聊 ( goroutine ) · 语雀 基于websocket的聊天室,可进一步参考: (1) go实现聊天室(WebSocket方式) (2) Golang代码搜集-基于websoc ...

  8. socket聊天室实现-群聊,私聊,好友列表,完整版

    效果图 登录.好友上线,下线均提示. 点击好友列表所有人,发送消息,既为群聊 点击好友列表,好友名字,既为选中此好友进行私聊 服务器端代码 ChatRoomServer package sram.se ...

  9. django+vue3实现websocket聊天室(群聊)

    1.如何实现聊天功能 2.什么是websocket 2.1解释什么叫全双工,半双工,单工 3.websocker的概念 4.websocket的优点 5.django ,vue如何实现websocke ...

  10. Node + WebSocket + Vue 聊天室创建群聊/加入群聊功能 – 第五章

    前言 本次算是做了一个小小的专题吧,"Nodejs + WebSocket + Vue实现聊天室功能",目前还在一步一步推进,之前已经可以一对一.一对多聊天了,今天就来创建群聊组, ...

最新文章

  1. 【另类见解】秒杀并非高不可攀
  2. MySQL 性能监控4大指标——第一部分
  3. 智能家居正是扎根好时节 蓄积且待春雨
  4. check_mk自定义监控实践之powershell
  5. Mybatis动态的添加删除列
  6. Java双向链表快速排序_双向链表的插入,删除,以及链表的快速排序
  7. SAP CRM Distribution Chain数据模型
  8. 代理服务器Tengine的研究与测试
  9. 软件设计师16-数据结构02(排序/查找)
  10. [題解]luogu_P2055假期的宿舍(二分圖最大匹配)
  11. python docx table 边框_使用pythondocx指定表中的边框外观
  12. 英伟达显卡不同架构_【英伟达NVIDIA显卡GPU架构核心代号名称一览】(截止到 2018·08)...
  13. knockoutjs的某些坑总结
  14. studio3t连接mysql_使用 Studio 3T 连接到 Azure Cosmos DB 的 API for MongoDB | Microsoft Docs
  15. 工业交换机和普通交换机有什么不同,又有哪些作用和优点
  16. 计算机病毒互助百科,病毒百科——计算机病毒分类
  17. 基于yolov3的水下目标检测(百度飞浆实现)
  18. 微信小程序canvas绘制圆形头像
  19. “知识共享”扎根中国,前景无量
  20. stata蒙特卡罗模拟(二)模拟中心极限定理

热门文章

  1. Couldn‘t find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  2. 2021线报天下 原创工具 (免费版本,无版权问题)
  3. 动态数据源,帆软报表同一个sql语句,根据不同的角色使用不同的连接
  4. 基于等效积分形式的近似方法——加权余量法(配点法,伽辽金法)求解微分方程近似解
  5. 多种文件上传绕过手法
  6. 如何在屏幕实时显示键盘操作(独家分享)
  7. ffmpeg推流 —— RTMP推流例程
  8. python 爬取种子_Python开发实例分享bt种子爬虫程序和种子解析
  9. IDEA 配置log4j
  10. php api权限,如何设计RESTful的API权限