-
设计题目及具体要求
编程题目:
设计并实现一个多线程的网络聊天系统,支持多用户及附加功能。
题目描述:
开发一个基于Java的网络聊天系统,该系统包含服务器端和客户端。系统应支持多用户同时在线,并提供一对一聊天、群聊、文件传输和用户状态显示等功能。
基本要求:
- 网络编程:
使用Java的Socket编程建立服务器端和客户端。
服务器端应能处理多个客户端的连接请求。
- 多线程实现:
服务器端使用多线程技术来同时管理多个客户端的请求。
每个客户端的连接应该在一个独立的线程中进行处理。
- 聊天功能:
实现用户间的一对一聊天功能。
用户可随时切换聊天对象
(可选)提供群聊功能,允许多个用户加入同一个聊天群组。
- 数据存储
使用MySQL数据库存储用户账户和密码,并实现:
- 用户注册和登录功能。
- 提供用户密码修改和找回功能。
- 聊天记录查看功能,可以选择查看与某个特定人的聊天记录
- 聊天记录的搜索功能,按照关键字搜索与某人的聊天记录
- 文件传输:
允许用户之间传输文件。
- 用户状态:
显示用户在线状态(在线、离线)。
- 用户界面:
客户端提供简洁的命令行界面,用户可以通过命令发送消息、传输文件和查看在线用户。
之前备考6级,把这个事给忘记了,二次大家催一下我哦!!!
还有,不要猜测我是谁,看我代码逻辑就知道我是个采集!!!
拿代码轻轻修改,不能完全一样哦!!!
拿了代码在我评论区下边轻轻的留个评论!!!谢谢
单身的爪子爱你们!!!
代码:
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
package Java_UserLog;
public class UserLog {
private String username;
private String password;
public UserLog() {
}
public UserLog(String username, String password) {
this.username = username;
this.password = password;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "UserLog{username = " + username + ", password = " + password + "}";
}
//z注册
public void userLogIn(){
}
}
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
package Java_UserLog;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private static List<ClientHandler> clients = new ArrayList<>();
public static void start() {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("客户端连接成功:" + clientSocket);
ClientHandler clientHandler = new ClientHandler(clientSocket);
clients.add(clientHandler);
Thread thread = new Thread(clientHandler);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void broadcastMessage(String message, ClientHandler excludeClient) {
for (ClientHandler client : clients) {
if (client != excludeClient) {
client.sendMessage(message);
}
}
}
public static void removeClient(ClientHandler client) {
clients.remove(client);
}
public static void showConnectedClients() {
System.out.println("当前连接到服务器端的客户端地址:");
for (ClientHandler client : clients) {
System.out.println(client.getClientAddress());
}
}
}
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
package Java_UserLog;
import java.io.*;
import java.net.Socket;
public class ClientHandler implements Runnable {
private Socket clientSocket;
private BufferedReader reader;
private PrintWriter writer;
public ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message) {
writer.println(message);
}
@Override
public void run() {
try {
String message;
while ((message = reader.readLine()) != null) {
System.out.println(clientSocket.getInetAddress()+"收到客户端消息:" + message);
// 对收到的消息进行处理,根据需求实现聊天和文件传输功能
if (message.equals("_FILE")) {
FileOutputStream fos = new FileOutputStream("C://Users//86159//IdeaProjects//ActionUse");
byte[] buffer = new byte[1024];
int length;
while ((length = reader.read()) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
//continue; // 跳过后续广播消息的处理
}
// 示例:广播消息给其他客户端
Server.broadcastMessage(message, this);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
clientSocket.close();
Server.removeClient(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getClientAddress() {
return clientSocket.getInetAddress().getHostAddress();
}
}
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
package Java_UserLog;
import java.io.*;
import java.net.Socket;
public class ClientHandler implements Runnable {
private Socket clientSocket;
private BufferedReader reader;
private PrintWriter writer;
public ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
try {
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message) {
writer.println(message);
}
@Override
public void run() {
try {
String message;
while ((message = reader.readLine()) != null) {
System.out.println(clientSocket.getInetAddress()+"收到客户端消息:" + message);
// 对收到的消息进行处理,根据需求实现聊天和文件传输功能
if (message.equals("_FILE")) {
FileOutputStream fos = new FileOutputStream("C://Users//86159//IdeaProjects//ActionUse");
byte[] buffer = new byte[1024];
int length;
while ((length = reader.read()) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
//continue; // 跳过后续广播消息的处理
}
// 示例:广播消息给其他客户端
Server.broadcastMessage(message, this);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
clientSocket.close();
Server.removeClient(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getClientAddress() {
return clientSocket.getInetAddress().getHostAddress();
}
}
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
package Java_UserLog;
import java.io.*;
import java.net.Socket;
import java.util.Arrays;
public class Client02 {
private static BufferedReader reader;
private static PrintWriter writer;
public static void main(String[] args) {
new Client().start();
}
public void start() {
try {
Socket socket = new Socket("localhost", 8888);
System.out.println("成功连接到服务器。");
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
// 启动接收消息的线程
Thread thread = new Thread(new MessageReceiver());
thread.start();
// 主线程负责发送消息
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String message = consoleReader.readLine();
writer.println(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class MessageReceiver implements Runnable {
@Override
public void run() {
try {
String message;
while ((message = reader.readLine()) != null) {
System.out.println("收到消息:" + message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void sendFile(String filePath) {
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
writer.println("_FILE"); // 发送文件传输的标志位
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
writer.write(Arrays.toString(buffer), 0, length);
}
writer.flush();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
《-----------可以添加更多的client对象和用户-------------》
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
package Java_UserLog;
import URLtest.InterAdress.Sever;
import java.io.*;
import java.net.Socket;
import java.sql.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/chatsystem?useSSL=false&serverTimezone=GMT";
String username = "root";
String password = "xiewang.mysql";
try {
// 加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 建立数据库连接
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("数据库链接成功");
// 执行数据库操作...
System.out.println("=================welcome to chat System!=========================");
System.out.println("------------you can choice those choises to achieve your goal!---");
System.out.println(" 1.log in(we meet first time,sign in a account!) ");
System.out.println(" 2.log up(sign up your account) ");
System.out.println(" 3.Retrieve password about forget your password ");
System.out.println("=================welcome to chat System!=========================");
int index = -1;
String username01;
String password01;
Scanner scanner = new Scanner(System.in);
index = scanner.nextInt();
switch (index){
case 1:
System.out.println("please create your username and password to use");
username01 = scanner.next();
System.out.println("please enable your password");
password01 = scanner.next();
String insertQuery = "INSERT INTO userconduct (username, password) VALUES (?, ?)";
PreparedStatement stmt = connection.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
String usernameValue = username01;
String passwordValue = password01;
stmt.setString(1, usernameValue);
stmt.setString(2, passwordValue);
int affectedRows = stmt.executeUpdate();
if (affectedRows > 0) {
ResultSet generatedKeys = stmt.getGeneratedKeys();
if (generatedKeys.next()) {
long id = generatedKeys.getLong(1);
System.out.println("Inserted record with ID: " + id);
}
}
break;
case 2:
System.out.println("Please enter your username and password to log in");
System.out.print("Username: ");
String loginUsername = scanner.next();
System.out.print("Password: ");
String loginPassword = scanner.next();
String selectQuery = "SELECT * FROM userconduct WHERE username = ? AND password = ?";
PreparedStatement loginStmt = connection.prepareStatement(selectQuery);
loginStmt.setString(1, loginUsername);
loginStmt.setString(2, loginPassword);
ResultSet loginResult = loginStmt.executeQuery();
if (loginResult.next()) {
System.out.println("Welcome to the system! You have successfully logged in.");
//登录进系统的下一步操作
System.out.println("=================welcome to chat System!=========================");
System.out.println(" 1 . begin talk!!! ");
System.out.println(" 2 . translate information by txt ");
System.out.println(" 3 . display client state ");
System.out.println(" 4 . alter your password! now you are in system");
System.out.println("=================welcome to chat System!=========================");
int logChoose = -1;
logChoose = scanner.nextInt();
boolean def = true;
/*while(def){
}*/
switch(logChoose){
case 1:
// 启动 Server 类
//Server server = new Server();
Server.start();
break;
case 2:
Server.start();
System.out.println("please input key words : _FILE " + "and put your address to send!");
String filePath = scanner.next(); // 替换为实际文件路径
System.out.println("send successfully!");
Client.sendFile(filePath);
break;
case 3:
//Server.start();
System.out.println("Client onLine:");
Server.showConnectedClients();
System.out.println("over!");
break;
case 4:
System.out.println("Please enter your username to retrieve password");
String retrieveUsername = scanner.next();
String checkQuery = "SELECT * FROM userconduct WHERE username = ?";
PreparedStatement checkStmt = connection.prepareStatement(checkQuery);
checkStmt.setString(1, retrieveUsername);
ResultSet checkResult = checkStmt.executeQuery();
if (checkResult.next()) {
System.out.println("Username found. Please enter a new password");
String newPassword = scanner.next();
String updateQuery = "UPDATE userconduct SET password = ? WHERE username = ?";
PreparedStatement updateStmt = connection.prepareStatement(updateQuery);
updateStmt.setString(1, newPassword);
updateStmt.setString(2, retrieveUsername);
int updateCount = updateStmt.executeUpdate();
if (updateCount > 0) {
System.out.println("Password updated successfully.");
} else {
System.out.println("Failed to update password.");
}
} else {
System.out.println("Username not found. Please try again.");
}
break;
default:
System.out.println("please input correctly! ");
}
} else {
System.out.println("Invalid username or password. Please try again.");
}
break;
case 3:
System.out.println("Please enter your username to retrieve password");
String retrieveUsername = scanner.next();
String checkQuery = "SELECT * FROM userconduct WHERE username = ?";
PreparedStatement checkStmt = connection.prepareStatement(checkQuery);
checkStmt.setString(1, retrieveUsername);
ResultSet checkResult = checkStmt.executeQuery();
if (checkResult.next()) {
System.out.println("Username found. Please enter a new password");
String newPassword = scanner.next();
String updateQuery = "UPDATE userconduct SET password = ? WHERE username = ?";
PreparedStatement updateStmt = connection.prepareStatement(updateQuery);
updateStmt.setString(1, newPassword);
updateStmt.setString(2, retrieveUsername);
int updateCount = updateStmt.executeUpdate();
if (updateCount > 0) {
System.out.println("Password updated successfully.");
} else {
System.out.println("Failed to update password.");
}
} else {
System.out.println("Username not found. Please try again.");
}
break;
default:
System.out.println("please input correctly");
}
// 关闭数据库连接
connection.close();
System.out.println("Disconnected from the database.数据库已关闭");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!!!!
-
系统测试
功能已全部实现完(部分图片)
拿代码,评论本爪子,已拿代码!!!!
老规矩,适当修改!
之前备考6级,把这个事给忘记了,二次大家催一下我哦!!!
还有,不要猜测我是谁,看我代码逻辑就知道我是个采集!!!
拿代码轻轻修改,不能完全一样哦!!!
拿了代码在我评论区下边轻轻的留个评论!!!谢谢
单身的爪子爱你们!!!
!!!
更多的检查你们自己来看喽!!!
感谢爪子!关注爪子!点赞!收藏!
大学生不容易兄弟们,看看已经“汗流浃背”的牢底吧
留下你们的作业,本爪子看看怎么个事!!!!!