Java-多人聊天小程序
上图啦!!!
首先运行:
Server.java
然后启动:
Client.java
最后退出客户端:
代码实现
Client
package chat;import javax.swing.*;public class Client {public static void main(String[] args) {// 使用Windows的界面风格try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");} catch (Exception e) {e.printStackTrace();}new TCPClient().start();}
}
Server
package chat;import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;public class Server implements ActionListener {private static final int SERVER_PORT = 2222;private ServerSocket server;private JFrame frame;private JPanel panel;private JTextArea textArea;private JScrollPane scrollPane;private JTextField textField;private JButton button;private boolean isStarted = false; // 服务器是否启动private List<ChatroomClient> clients = new ArrayList<ChatroomClient>();public Server() {frame = new JFrame("服务器");panel = new JPanel();textArea = new JTextArea();textArea.setEditable(false);textArea.setLineWrap(true);scrollPane = new JScrollPane(textArea);scrollPane.setBounds(5, 5, 275, 290);scrollPane.setBorder(BorderFactory.createLoweredSoftBevelBorder());textField = new JTextField();textField.setBounds(6, 305, 210, 30);button = new JButton("发送");button.setBounds(220, 305, 60, 30);button.addActionListener(this);textField.addActionListener(this);panel.add(scrollPane);panel.add(textField);panel.add(button);panel.setLayout(null);frame.add(panel);frame.setSize(300, 400);frame.setLocation((1366 - 640) >> 1, (768 - 400) >> 1);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {// 使用Windows的界面风格try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");} catch (Exception e) {e.printStackTrace();}new Server().serverStart();}public void serverStart() {while (!isStarted) { // 无限循环尝试启动服务器try {server = new ServerSocket(SERVER_PORT);isStarted = true;Thread.sleep(500);} catch (BindException e) {textArea.setText("端口2222被占用,请关闭对应的程序!\n");} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}textArea.append("服务器启动,等待客户端连接……\n");try {// 无限循环检测是否有客户端加入,有则加到listwhile (isStarted) {Socket s = server.accept();ChatroomClient client = new ChatroomClient(s);clients.add(client);client.start();}} catch (IOException e) {e.printStackTrace();} finally {try {server.close();} catch (IOException e) {e.printStackTrace();}}}// 服务器发送消息public void send() {if (textField.getText().length() < 1) { // 没有输入东西return;} else if (clients.size() == 0) {textArea.append("无客户端连接!\n");return;}SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String s = "服务器 " + dateFormat.format(new Date()) + "\n " + textField.getText() + "\n";textArea.append(s);// 循环给列表中的客户端发送信息for (int i = 0; i < clients.size(); ++i) {ChatroomClient client = clients.get(i);client.send(s);}textField.setText(null);}public void actionPerformed(ActionEvent arg0) {send();}// Inner Class ChatroomClientclass ChatroomClient extends Thread {private Socket client;private DataInputStream in;private DataOutputStream out;//private boolean isConnected = false;// 客户端初始化public ChatroomClient(Socket client) {this.client = client;try {in = new DataInputStream(client.getInputStream());out = new DataOutputStream(client.getOutputStream());} catch (IOException e) {e.printStackTrace();}SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");textArea.append("客户端已连接 " + dateFormat.format(new Date()) + "\n");textArea.append("地址:" + client.getInetAddress().getHostAddress() + "\n端口:" + client.getPort() + "\n\n");}// 发送消息public void send(String str) {try {out.writeUTF(str);out.flush();} catch (IOException e) {clients.remove(this);textArea.append("客户端已退出,从列表删除!\n");}}public void run() {try {while (true) {textArea.setCaretPosition(textArea.getDocument().getLength()); // 显示JTextArea最末String str = in.readUTF();//str = str.substring(0, 3) + (clients.indexOf(this) + 1) + str.substring(3);textArea.append(str);for (int i = 0; i < clients.size(); ++i) {ChatroomClient client = clients.get(i);client.send(str);}Thread.sleep(100); // 每100ms读取对方信息一次}} catch (IOException e) {textArea.append("客户端断开连接!\n");clients.remove(this);} catch (InterruptedException e) {e.printStackTrace();} finally {clients.remove(this);try {if (in != null)client.close();if (out != null)client.close();if (client != null)client.close();} catch (IOException e) {e.printStackTrace(); } } } } }
TCPClient
package chat;import java.awt.Color;
import java.awt.event.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;class TCPClient extends Thread implements ActionListener, MouseListener {private String des = "127.0.0.1"; // 服务器地址private int port = 2222; // 服务器端口private Socket client;private DataInputStream in;private DataOutputStream out;private JFrame frame;private JPanel panel;private JTextArea textArea;private JScrollPane scrollPane;private JTextField textField;private JTextField tfDes, tfPort;private JLabel label1, label2, addrChange;private JButton button;private boolean isConnected = false; // 是否连接上public TCPClient() {frame = new JFrame("客户端");panel = new JPanel();textArea = new JTextArea();textArea.setEditable(false);textArea.setLineWrap(true);scrollPane = new JScrollPane(textArea);scrollPane.setBounds(5, 5, 275, 290);scrollPane.setBorder(BorderFactory.createLoweredSoftBevelBorder());textField = new JTextField();textField.setBounds(6, 305, 210, 30);textField.addActionListener(this);button = new JButton("发送");button.setBounds(220, 305, 60, 30);button.addActionListener(this);label1 = new JLabel("服务器地址:");label1.setBounds(10, 340, 75, 20);tfDes = new JTextField(des);tfDes.setBounds(75, 341, 80, 20);tfDes.setBorder(null);label2 = new JLabel("端口:");label2.setBounds(160, 340, 40, 20);tfPort = new JTextField(Integer.toString(port));tfPort.setBounds(190, 341, 35, 20);tfPort.setBorder(null);addrChange = new JLabel("连接");addrChange.setForeground(Color.BLUE);addrChange.setBounds(235, 337, 30, 20);addrChange.setBorder(BorderFactory.createRaisedBevelBorder());addrChange.addMouseListener(this);panel.add(scrollPane);panel.add(textField);panel.add(button);panel.add(label1);panel.add(tfDes);panel.add(label2);panel.add(tfPort);panel.add(addrChange);panel.setLayout(null);frame.add(panel);frame.setSize(300, 400);frame.setLocation(1366 >> 1, (768 - 400) >> 1);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}// 发送消息public void send() {if (textField.getText().length() < 1) { // 没有输入东西return;} else if (out == null || client.isClosed()) {textArea.append("没有与服务器连接!\n");return;}try {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");out.writeUTF(InetAddress.getLocalHost().getHostName() + " " + dateFormat.format(new Date()) + "\n " + textField.getText() + "\n");out.flush();//textArea.append("客户端 " + new Date().toLocaleString() + "\n " + textField.getText() + "\n");textField.setText(null);} catch (IOException e) {e.printStackTrace();}}public void actionPerformed(ActionEvent arg0) {send();}public void mouseClicked(MouseEvent arg0) {}public void mouseEntered(MouseEvent arg0) {}public void mouseExited(MouseEvent arg0) {}public void mousePressed(MouseEvent arg0) {addrChange.setBorder(BorderFactory.createLoweredBevelBorder());}public void mouseReleased(MouseEvent arg0) {addrChange.setBorder(BorderFactory.createRaisedBevelBorder());if (!tfDes.getText().matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")|| !tfPort.getText().matches("[0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]")) {textArea.append("请输入正确的地址和端口!!\n");return;}String tmpd = tfDes.getText();int tmpp = Integer.parseInt(tfPort.getText());if (des.equals(tmpd) && port == tmpp) {if (isConnected == true)textArea.append("已经和指定服务器连上,请不要重复连接!\n");return;}des = tmpd;port = tmpp;try {if (in != null)in.close();if (out != null)out.close();if (client != null)client.close();} catch (IOException e) {e.printStackTrace();}if (isConnected == false) {textArea.append("正在连接服务器……\n");textArea.append("目标地址:" + des + "\n");textArea.append("目标端口:" + port + "\n");} else {textArea.append("用户中断当前连接!\n");}isConnected = false;}public void run() {while (true) {textArea.append("正在连接服务器……\n");textArea.append("目标地址:" + des + "\n");textArea.append("目标端口:" + port + "\n");while (!isConnected) {try {client = new Socket(des, port);in = new DataInputStream(client.getInputStream());out = new DataOutputStream(client.getOutputStream());isConnected = true;Thread.sleep(500);} catch (IOException e) {continue;} catch (InterruptedException e) {e.printStackTrace();}}SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");textArea.append("连接服务器成功!" + dateFormat.format(new Date()) + "\n\n");try {while (isConnected) {textArea.setCaretPosition(textArea.getDocument().getLength()); // 显示JTextArea最末textArea.append(in.readUTF()); // 每100ms读取对方信息一次Thread.sleep(100);}} catch (IOException e) {textArea.append("与服务器失去连接!\n\n");} catch (InterruptedException e) {e.printStackTrace();} finally {try {if (in != null)in.close();if (out != null)out.close();if (client != null)client.close();} catch (IOException e) {e.printStackTrace();}isConnected = false;}}}}
Java-多人聊天小程序相关推荐
- javaweb通过iis实现域账号免登陆_聊天小程序的Java实现
登陆界面 注册账号 找回密码 重设密码 聊天界面 多人聊天 一. 设计任务 1.1设计意义 <Java基础入门>课程设计是对学生的一种全面综合训练,它包括问题分析,用户界面设计,程序设计基 ...
- java中的jgroup_JGroups实现聊天小程序
本文实例为大家分享了JGroups实现聊天小程序的具体代码,供大家参考,具体内容如下 效果图: 代码部分: package com.lei.jgoups; import java.io.Buffere ...
- Java的网络编程【TCP与UDP聊天小程序】
Java的网络编程[TCP与UDP聊天小程序] 1. TCP协议 1.1 传输控制协议(Transmission Control Protocol),是一种**面向连接(全程保持连接)**的协议,类似 ...
- 项目展示--基于UDP传输协议+GUI的Java聊天小程序(图片加源代码)
前言 前几天没什么事儿,在学习UDP和TCP传输协议,顺手做了一个基于UDP协议的聊天小程序,同时复习了Java的GUI知识. 程序运行截图展示 1.运行程序,生成第一个窗口,输入要监听的端口号:12 ...
- 使用chatgpt实现微信聊天小程序(秒回复),github开源(附带链接)
文章目录 前言 效果展示 原理说明 服务器端代码说明 微信小程序代码说明 代码链接 总结 前言 我在前一段时间突发奇想,就使用java来调用chatgpt的接口,然后写了一个简单小程序,也上了热榜第一 ...
- 付费社群聊天小程序V1.4.5+前端
简介: 付费社群聊天小程序V1.4.5+前端 备注:不用重新上传小程序1,修复购买会员分享分佣BUG社群小程序,通过微信群裂变,社群营销,知识付费以及社群电商可实现粉丝营销闭环,玩转社群运营.一.自 ...
- 计算器小程序java课程设计,java课程设计----计算器小程序报告.doc
java课程设计----计算器小程序报告.doc #####学院JAVA语言课程设计报告小程序计算器系统管理班级:#######姓名:#####指导老师:###时间:2012年6月25日至6月29日2 ...
- 基于JAVA学习自律养成小程序前台.mp4计算机毕业设计源码+系统+数据库+lw文档+部署
基于JAVA学习自律养成小程序前台.mp4计算机毕业设计源码+系统+数据库+lw文档+部署 基于JAVA学习自律养成小程序前台.mp4计算机毕业设计源码+系统+数据库+lw文档+部署 本源码技术栈: ...
- 2022-2025年最新最全Java毕业设计选题Java/mysql/springboot/微信小程序
看中选题可私信博主获取源码和论文,点击选题可以查看演示视频以及功能预览 主页更多项目 1.前言 好的毕设题目能够让你花最少的时间和精力获取最优的结果,不知道怎么选题,可以参考下面选题 选题指导:不知道 ...
最新文章
- Redis添加密码认证Cacti监控读取Redis状态值为 -1 的最快速解决方案
- SAP UI5 xml view content parse
- qt-项目部署(某些情况下编译器中运行异常的话可以使用命令windeployqt 程序名在安装的qt dos命令下补全部分依赖库在运行项目或发布)
- 聊聊storm的PartialKeyGrouping
- 6本书,读懂2022年最火的边缘计算
- 从service启动activity startActivity慢 的解决方案
- Xp账号变成temp
- 编译原理--运行时存储组织(自己看)
- [每日更新-MySQL基础]-1.认识MySQL
- 遗传算法是机器学习算法嘛?_基于遗传算法的机器人控制器方法
- Java是否存在内存泄露
- 留言系统php课程设计,PHP课程设计网络留言板
- Stacked Hourglass Networks for Human Pose Estimation
- 地磅系统——车辆识别系统的自动化管理
- android xml 圆形图片,Android ImageView实现圆角,圆形图片
- 研究区域内测高卫星数据选取(pass)--以T/P-Jason1/2/3为例
- 第1章 持续交付2.0
- mysql数据库怎么导出导入表
- 我的开源项目--华尔街见闻(仿)
- 装完服务器打补丁,看看有谁比我更无聊~