零、我使用了什么AI工具辅助选题
无。
一、选题与前期调查。
选题:网络通信系统
用户可以在客户端进行注册账号,与他人私聊,发送离线消息,以及文件。也可以群发消息,以及安全下线。
管理员可以向所有用户进行推送消息。
二、系统功能设计
客户端:用户注册 私聊 离线消息 登录 群发 发送文件
服务端:推送消息
三、数据如何存储:文件、数据库、内存。
文件
四、界面:GUI、控制台、Web?
控制台
五、开发工具:Eclipse, NetBeans, JUnit, git, 阿里巴巴代码规约IDE插件、AI工具。
IDEA
六、包结构
服务端
客户端
七、部分代码
SeverConnectClientThread
package com.wwb.service;
import com.wwb.message.Message;
import com.wwb.message.MessageType;
import com.wwb.user.LodeAllUsers;
import com.wwb.user.OfflineInformationList;
import com.wwb.user.StoringFilesMap;
import com.wwb.user.User;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
public class ServerConnectClientThread extends Thread{
private Socket socket;
private User user;
public ServerConnectClientThread(Socket socket, User user) {
this.socket = socket;
this.user = user;
}
public User getUser() {
return user;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
if(OfflineInformationList.containsKey(user.getUserId())){
ArrayList<Message> offlineInformationList = OfflineInformationList.getListByUserId(user.getUserId());
for (Message message : offlineInformationList) {
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
while(true) {
try {
if(StoringFilesMap.getStoringFilesMap().containsKey(user.getUserId())){
ArrayList<Message> fileList = StoringFilesMap.getFileList(user.getUserId());
for (Message message1 : fileList) {
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message1);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
Message message = (Message) objectInputStream.readObject();
//测试
System.out.println(message.getMesType());
//获取当前在线用户列表
if (message.getMesType().equals(MessageType.MESSAGE_ALL_ONLINE_USERS)) {
System.out.println("用户:" + user.getName() + " 正在获取在线用户列表");
String content = ManageClientThread.getAllOnlineUsers();
Message message1 = new Message();
message1.setSender(user.getName());
message1.setContent(content);
message1.setMesType(MessageType.MESSAGE_ALL_ONLINE_USERS);
message1.setLocalDateTime(LocalDateTime.now());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message1);
//发送消息给某用户
} else if (message.getMesType().equals(MessageType.MESSAGE_SEND_MESS_TO_ONE)) {
//如果用户在线
if (ManageClientThread.containKey(message.getGetter())) {
System.out.println(ManageClientThread.getByUserId(message.getSender()).user.getName() + " 正在给 " + ManageClientThread.getByUserId(message.getSender()).user.getName() + "私发消息");
ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(ManageClientThread.getByUserId(message.getGetter()).socket.getOutputStream());
Message message1 = new Message();
message1.setGetter(ManageClientThread.getByUserId(message.getGetter()).user.getName());
message1.setSender(ManageClientThread.getByUserId(message.getSender()).user.getName());
message1.setMesType(MessageType.MESSAGE_RETURN_MESS_GETTER_ONE);
message1.setLocalDateTime(message.getLocalDateTime());
message1.setContent(message.getContent());
objectOutputStream1.writeObject(message1);
Message message2 = new Message();
message2.setLocalDateTime(message.getLocalDateTime());
message2.setContent("用户:" + ManageClientThread.getByUserId(message.getGetter()).user.getName() + " 在线.\n" +
"你 对 " + ManageClientThread.getByUserId(message.getGetter()).user.getName() + " 说:" + message.getContent());
message2.setMesType(MessageType.MESSAGE_RETURN_MESS_SENDER_ONE);
ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream2.writeObject(message2);
//如果用户不在线
} else {
System.out.println(ManageClientThread.getByUserId(message.getSender()).user.getName() + " 正在给 " + LodeAllUsers.getAllUsersMap().get(message.getGetter()).getName() + "发送离线消息");
Message message1 = new Message();
message1.setLocalDateTime(message.getLocalDateTime());
message1.setContent("用户:" + LodeAllUsers.getAllUsersMap().get(message.getGetter()).getName() + " 不在线,发送离线消息\n" +
"你 对 " + LodeAllUsers.getAllUsersMap().get(message.getGetter()).getName() + " 说 " + message.getContent());
message1.setMesType(MessageType.MESSAGE_RETURN_MESS_SENDER_ONE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message1);
Message message2 = new Message();
message2.setSender(LodeAllUsers.getAllUsersMap().get(message.getSender()).getName());
message2.setGetter(message.getGetter());
message2.setContent(message.getContent());
message2.setLocalDateTime(message.getLocalDateTime());
message2.setMesType(MessageType.MESSAGE_RETURN_MESS_GETTER_ONE);
OfflineInformationList.addOfflineInformation(message2.getGetter(), message2);
}
}else if (message.getMesType().equals(MessageType.MESSAGE_SEND_MESS_TO_ALL)){
System.out.println(ManageClientThread.getByUserId(message.getSender()).user.getName() + "正在群发消息");
Message message1 = new Message();
message1.setSender(ManageClientThread.getByUserId(message.getSender()).user.getName());
message1.setMesType(MessageType.MESSAGE_RETURN_MESS_GETTER_ALL);
message1.setContent(message.getContent());
message1.setLocalDateTime(message.getLocalDateTime());
ConcurrentHashMap<String, ServerConnectClientThread> allClientThread = ManageClientThread.getAllClientThread();
Collection<ServerConnectClientThread> values = allClientThread.values();
for (ServerConnectClientThread value : values) {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(value.socket.getOutputStream());
objectOutputStream.writeObject(message1);
}
Message message2 = new Message();
message2.setLocalDateTime(message.getLocalDateTime());
message2.setContent("你对大家说: " + message.getContent());
message2.setMesType(MessageType.MESSAGE_RETURN_MESS_SENDER_ALL);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message2);
} else if (message.getMesType().equals(MessageType.MESSAGE_SEND_FILE_TO_ONE)) {
System.out.println(LodeAllUsers.getAllUsersMap().get(message.getSender()).getName() + " 给 " + LodeAllUsers.getAllUsersMap().get(message.getGetter()).getName() + " 发送了以一个文件");
Message message1 = new Message();
message1.setSender(LodeAllUsers.getAllUsersMap().get(message.getSender()).getName());
message1.setGetter(LodeAllUsers.getAllUsersMap().get(message.getGetter()).getName());
message1.setMesType(MessageType.MESSAGE_SEND_FILE_RET_SENDER);
message1.setLocalDateTime(message.getLocalDateTime());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message1);
Message message2 = new Message();
message2.setLocalDateTime(message.getLocalDateTime());
message2.setFileBytes(message.getFileBytes());
message2.setSender(LodeAllUsers.getAllUsersMap().get(message.getSender()).getName());
message2.setGetter(LodeAllUsers.getAllUsersMap().get(message.getGetter()).getName());
message2.setMesType(MessageType.MESSAGE_SEND_FILE_RET_GETTER);
StoringFilesMap.addFile(message.getGetter(), message2);
} else if(message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
System.out.println(message.getSender() + " 安全退出网络通信系统");
Message message1 = new Message();
message1.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message1);
ManageClientThread.removeServerConnectClientThread(message.getSender());
socket.close();
break;
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
ClientConnectServerThread
package com.wwb.commclient.service;
import com.wwb.message.Message;
import com.wwb.message.MessageType;
import java.io.*;
import java.net.Socket;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class ClientConnectServerThread extends Thread{
private Socket socket;
public Scanner scanner = new Scanner(System.in);
public ClientConnectServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
System.out.println("客户端线程,等待读取服务端发送信息");
try {
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
Message message = (Message) objectInputStream.readObject();
if(message.getMesType().equals(MessageType.MESSAGE_ALL_ONLINE_USERS)){
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("在线用户如下:");
String[] s = message.getContent().split(" ");
for (String string : s) {
System.out.println(string);
}
}else if(message.getMesType().equals(MessageType.MESSAGE_RETURN_MESS_SENDER_ONE)){
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(message.getContent());
}else if(message.getMesType().equals(MessageType.MESSAGE_RETURN_MESS_GETTER_ONE)){
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(message.getSender() + " 对你说: " + message.getContent());
} else if (message.getMesType().equals(MessageType.MESSAGE_RETURN_MESS_SENDER_ALL)){
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(message.getContent());
} else if (message.getMesType().equals(MessageType.MESSAGE_RETURN_MESS_GETTER_ALL)) {
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println(message.getSender() + " 对大家说: " + message.getContent());
} else if(message.getMesType().equals(MessageType.MESSAGE_TO_ALL_CLIENT_MES)){
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("服务器为大家推送消息: " + message.getContent());
}else if(message.getMesType().equals(MessageType.MESSAGE_SEND_FILE_RET_SENDER)){
System.out.println("时间: " + message.getLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("你给 " + message.getGetter() + " 发送了一个文件");
}else if(message.getMesType().equals(MessageType.MESSAGE_SEND_FILE_RET_GETTER)){
System.out.println(message.getSender() + " 给你发送了一个文件");
System.out.println("请输入存储文件的路径");
String filePath = scanner.next();
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(message.getFileBytes());
fileOutputStream.close();
System.out.println("存储成功");
}else if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {
socket.close();
break;
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
MessageType
package com.wwb.message;
/**
* 消息类型
*/
public class MessageType {
public static final String MESSAGE_USER_NOT_FOUND = "1";
public static final String MESSAGE_LOGIN_SUCCESSFUL = "2";
public static final String MESSAGE_LOGIN_FAILURE = "3";
public static final String MESSAGE_ALL_ONLINE_USERS = "4";
public static final String MESSAGE_SEND_MESS_TO_ONE = "5";
public static final String MESSAGE_RETURN_MESS_GETTER_ONE = "6";
public static final String MESSAGE_RETURN_MESS_SENDER_ONE = "7";
public static final String MESSAGE_SEND_MESS_TO_ALL = "8";
public static final String MESSAGE_RETURN_MESS_SENDER_ALL = "9";
public static final String MESSAGE_RETURN_MESS_GETTER_ALL = "10";
public static final String MESSAGE_CLIENT_EXIT = "11";
public static final String MESSAGE_REGISTER_ACCOUNT = "12";
public static final String MESSAGE_TO_ALL_CLIENT_MES = "13";
public static final String MESSAGE_SEND_FILE_TO_ONE = "14";
public static final String MESSAGE_SEND_FILE_RET_SENDER = "15";
public static final String MESSAGE_SEND_FILE_RET_GETTER = "16";
}
UserClientServer
package com.wwb.commclient.service;
import com.wwb.message.Message;
import com.wwb.message.MessageType;
import com.wwb.user.User;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.time.LocalDateTime;
/**
* 为客户端用户提供服务
*/
public class UserClientService {
User user = new User();
Socket socket = null;
public User checkUser(String userId, String password){
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 8888);
user.setUserId(userId);
user.setPassword(password);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(user);
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
Message message1 = (Message)objectInputStream.readObject();
//登录失败
if(message1.getMesType().equals(MessageType.MESSAGE_LOGIN_FAILURE)){
System.out.println("用户名或者密码错误");
socket.close();
return null;
}//该用户未注册账号
else if(message1.getMesType().equals(MessageType.MESSAGE_USER_NOT_FOUND)){
System.out.println("该用户未注册,请先注册账号");
socket.close();
return null;
}//登录成功
else if (message1.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCESSFUL)) {
System.out.println("登录成功");
user = new User();
user.setName(message1.getContent());
user.setUserId(userId);
user.setPassword(password);
ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(socket);
clientConnectServerThread.start();
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
return user;
}
public void registerAccount(String userId, String password){
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 8888);
user.setUserId(userId);
user.setPassword(password);
user.setName("未注册");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(user);
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
Message message1 = (Message)objectInputStream.readObject();
if(message1.getMesType().equals(MessageType.MESSAGE_REGISTER_ACCOUNT)){
System.out.println("注册成功");
}
user.setName(message1.getContent());
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
public void getOnlineUsersList(){
Message message = new Message();
message.setMesType(MessageType.MESSAGE_ALL_ONLINE_USERS);
message.setSender(user.getName());
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void senderMessageToOne(String getterId, String content){
Message message = new Message();
message.setContent(content);
message.setSender(user.getUserId());
message.setGetter(getterId);
message.setLocalDateTime(LocalDateTime.now());
message.setMesType(MessageType.MESSAGE_SEND_MESS_TO_ONE);
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void senderMessageToAll(String content){
Message message = new Message();
message.setContent(content);
message.setSender(user.getUserId());
message.setLocalDateTime(LocalDateTime.now());
message.setMesType(MessageType.MESSAGE_SEND_MESS_TO_ALL);
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void clientExit(){
Message message = new Message();
message.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
message.setSender(user.getUserId());
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message);
System.out.println(user.getUserId() + " 退出系统");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void sendFileToOne(String filePath, String userId){
File file = new File(filePath);
byte[] fileBytes = new byte[(int) new File(filePath).length()];
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
bufferedInputStream.read(fileBytes);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Message message = new Message();
message.setLocalDateTime(LocalDateTime.now());
message.setMesType(MessageType.MESSAGE_SEND_FILE_TO_ONE);
message.setFileBytes(fileBytes);
message.setGetter(userId);
message.setSender(user.getUserId());
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(message);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
CommServer
package com.wwb.service;
import com.wwb.Administrator.AdministratorThread;
import com.wwb.message.Message;
import com.wwb.message.MessageType;
import com.wwb.user.LodeAllUsers;
import com.wwb.user.User;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;
public class CommServer {
ConcurrentHashMap<String, User> allUsers = LodeAllUsers.getAllUsersMap();
public static void main(String[] args) {
new CommServer();
}
public CommServer() {
ServerSocket serverSocket = null;
new AdministratorThread().start();
try {
serverSocket = new ServerSocket(8888);
LodeAllUsers.loadAllUsers();
allUsers = LodeAllUsers.getAllUsersMap();
System.out.println("服务端正在 8888 端口监听 等待用户访问");
while(true){
Socket socket = serverSocket.accept();
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
User user = (User) objectInputStream.readObject();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
Message message = new Message();
if("未注册".equals(user.getName())){
System.out.println("用户: " + user.getUserId() + " 正在注册账号");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src//AllUsersInformation.txt", true));
bufferedWriter.write("\n" + user.getUserId() + " " + user.getUserId() + " " + user.getPassword());
bufferedWriter.close();
message.setMesType(MessageType.MESSAGE_REGISTER_ACCOUNT);
message.setContent(user.getUserId());
objectOutputStream.writeObject(message);
LodeAllUsers.loadAllUsers();
allUsers = LodeAllUsers.getAllUsersMap();
socket.close();
}
else if(!allUsers.containsKey(user.getUserId())){
System.out.println("该用户未注册账号");
message.setMesType(MessageType.MESSAGE_USER_NOT_FOUND);
objectOutputStream.writeObject(message);
socket.close();
} else if (!allUsers.get(user.getUserId()).getPassword().equals(user.getPassword())) {
System.out.println("用户 " + allUsers.get(user.getUserId()).getName() + " 输入密码错误");
message.setMesType(MessageType.MESSAGE_LOGIN_FAILURE);
objectOutputStream.writeObject(message);
socket.close();
} else {
System.out.println("用户 " + allUsers.get(user.getUserId()).getName() + " 登录成功");
message.setMesType(MessageType.MESSAGE_LOGIN_SUCCESSFUL);
message.setContent(allUsers.get(user.getUserId()).getName());
objectOutputStream.writeObject(message);
ServerConnectClientThread serverConnectClientThread = new ServerConnectClientThread(socket, allUsers.get(user.getUserId()));
serverConnectClientThread.start();
ManageClientThread.addServerConnectClientThread(user.getUserId(), serverConnectClientThread);
}
}
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
}