java多线程控制台聊天室(转)
2024-10-28 19:43:33
用java多线程实现一个控制台聊天室,呵呵,好玩!
聊天室服务器端
聊天室服务器端
- package tf.thread;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ChatServer {
- private static final int PORT = 8888; // 端口号
- private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象
- private ExecutorService exec;
- private ServerSocket server;
- public static void main(String[] args) {
- new ChatServer();
- }
- public ChatServer() {
- try {
- server = new ServerSocket(PORT);
- exec = Executors.newCachedThreadPool();
- System.out.println("服务器已启动!");
- Socket client = null;
- while (true) {
- client = server.accept(); // 接收客户连接
- list.add(client);
- exec.execute(new ChatTask(client));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- static class ChatTask implements Runnable {
- private Socket socket;
- private BufferedReader br;
- private PrintWriter pw;
- private String msg;
- public ChatTask(Socket socket) throws IOException {
- this.socket = socket;
- br = new BufferedReader(new InputStreamReader(socket
- .getInputStream()));
- msg = "【" + this.socket.getInetAddress() + "】进入聊天室!当前聊天室有【"
- + list.size() + "】人";
- sendMessage();
- }
- public void run() {
- try {
- while ((msg = br.readLine()) != null) {
- if (msg.trim().equals("bye")) {
- list.remove(socket);
- br.close();
- pw.close();
- msg = "【" + socket.getInetAddress() + "】离开聊天室!当前聊天室有【"
- + list.size() + "】人";
- socket.close();
- sendMessage();
- break;
- } else {
- msg = "【" + socket.getInetAddress() + "】说:" + msg;
- sendMessage();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 群发消息给聊天室的所有人
- */
- private void sendMessage() throws IOException {
- System.out.println(msg);
- for (Socket client : list) {
- pw = new PrintWriter(client.getOutputStream(), true);
- pw.println(msg);
- }
- }
- }
- }
聊天室客户端(输入bye可退出聊天室)
- package tf.thread;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ChatClient {
- private static final int PORT = 8888;
- private static ExecutorService exec = Executors.newCachedThreadPool();
- public static void main(String[] args) throws Exception {
- new ChatClient();
- }
- public ChatClient() {
- try {
- Socket socket = new Socket("192.168.1.120", PORT);
- exec.execute(new Sender(socket));
- System.out
- .println("【" + socket.getInetAddress() + "】您好,欢迎来到阿飞聊天室!");
- BufferedReader br = new BufferedReader(new InputStreamReader(socket
- .getInputStream()));
- String msg;
- while ((msg = br.readLine()) != null) {
- System.out.println(msg);
- }
- } catch (Exception e) {
- }
- }
- /**
- * 客户端线程获取控制台输入消息
- */
- static class Sender implements Runnable {
- private Socket socket;
- public Sender(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
- String msg;
- while (true) {
- msg = br.readLine();
- pw.println(msg);
- if (msg.trim().equals("bye")) {
- pw.close();
- br.close();
- exec.shutdownNow();
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者
mina 几句就搞定了的
http://www.iteye.com/topic/365764
最新文章
- 书柜的尺寸(bzoj 1933)
- ES6笔记系列
- CodeForces 455D 分块
- Linux下错误的捕获:全局变量errno和strerror()
- noi 666 放苹果
- POJ 1523 SPF(寻找关节点)
- storm的数据源编程单元Spout学习整理
- 基于UltraVNC实现客户端远程控制
- 关于javascript模式一书中var white = new Array(256).join(&ldquo; &rdquo;)
- Pascal Game Development with Jason McMillen
- POJ 3169 Layout 差分约束系统
- 2015第24周一Spring事务
- mac Path had bad ownership/permissions
- 如何判断浏览器为ie10以上
- KAGGLE竟赛
- linux下动态链接库(.so)的显式调用和隐式调用
- AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造
- java8_api_字符串处理
- Java7后try语句的优化
- Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】
热门文章
- [ACM] n划分数m部分,它要求每一个部分,并采取了最大的产品(间隔DP)
- 基于visual Studio2013解决C语言竞赛题之1091多项式
- 金蝶盘点机条码数据採集器PDA,WIFI已经连接,可是PDA应用程序还是网络初始化不成功?
- 使用BigDecimal来进行精确计算
- C++基础学习笔记----第七课(面向对象的基本概念)
- 8天玩转并行开发——第八天 用VS性能向导解剖你的程序
- eval 捕获错误
- UltraEdit配置python和lua环境
- CDialogBar(对话条)和CReBar(伸缩条)的编程
- 四张类图理一下Streams的用法