java实现聊天软件Word文件下载.docx
《java实现聊天软件Word文件下载.docx》由会员分享,可在线阅读,更多相关《java实现聊天软件Word文件下载.docx(30页珍藏版)》请在冰点文库上搜索。
![java实现聊天软件Word文件下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/4/93dd8595-5ea4-4168-8de1-cf67a4be65b8/93dd8595-5ea4-4168-8de1-cf67a4be65b81.gif)
菜单界面
主菜单:
登录成功菜单:
聊天菜单:
在线聊天:
功能介绍
登录功能
用户输入用户名和密码,通过对数据库中的注册信息查询,如果用户名和密码存在,则登陆成功;
如果不存在,提示输入错误,返回主菜单,重新输入用户名和密码进行登录。
注册功能
用户输入注册信息,用户名和密码,通过对数据库的信息的查询确认用户名未存在,然后将注册信息存储在数据库中,同时提示注册成功,返回主菜单,选择其他功能;
如果用户名已存在,提示用户名重复,请重新注册,返回主菜单重新进行注册。
聊天功能
聊天
显示在线用户,输入在线用户用户名,输入消息的内容,发送。
群聊
对所有注册用户发布消息,在线用户可以立刻接受到消息,通过对注册用户和在线的用户的比较显示出离线用户,将离线用户的信息和消息内容进行存储,离线用户登录之后通过对离线消息的存储判断是否有离线消息,如果有自动发送,然后返回主菜单。
返回主菜单
用户选择返回主菜单功能,可以跳转到主菜单重新进行其他的操作。
退出功能
用户选择退出功能,服务器接收到命令之后断开连接,返回客户端断开成功,客户端接收到断开成功的消息同时也断开连接,客户端实现下线。
关键技术
1.TCP协议实现了客户端和服务器的连接。
2.输入输出流实现服务器与客户端之间信息的交流。
3.多线程实现了多个线程的同时运行,通过同步块解决内存不同步的问题。
4.JDBC实现对数据库的连接和调用,SQL语句实现数据的插入和查询。
5.HashMap实现对在线用户连接信息的存储,String类实现对命令的处理Scanner类实现键盘的输入
源代码
客户端:
packagejavaNet;
importjava.io.IOException;
import.*;
importjava.util.Scanner;
publicclassClient{
publicbooleanisLogin=false;
privateStringserverIP="
192.168.191.3"
;
privateintport=10000;
privateSocketsocket=null;
privateClientConnectionconnetion;
publicClient(StringserverIP,intport)throwsUnknownHostException,
IOException{
this.serverIP=serverIP;
this.port=port;
this.socket=newSocket(serverIP,port);
this.connetion=newClientConnection(this.socket);
}
publicvoidsendMsg(Stringmsg){
this.connetion.sendMessage(msg);
publicclassClientConnectionextendsChatConnection{
privateStringselfName;
publicClientConnection(Socketsocket)throwsIOException{
super(socket);
}
@Override
publicvoidprocess(Stringmessage){
finalString[]pars=message.split("
/"
);
if(pars!
=null&
&
pars.length>
0){
Stringorder=pars[0];
if(order.equals("
msg"
)){
StringfromUser=pars[1];
Stringwords=pars[3];
System.out.println(fromUser+"
:
"
+words);
printMainMenu(isLogin);
}elseif(order.equals("
login"
newThread(newRunnable(){
@Override
publicvoidrun(){
System.out.println(pars[2]);
if(pars[1].equals("
true"
isLogin=true;
printMainMenu(isLogin);
intmainid=readInt();
try{
processMainMenu(mainid);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}else{
try{
processMainMenu(mainid);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}).start();
backRigister"
newThread(newRunnable(){
System.out.println(pars[1]);
intmainid=readInt();
try{
processMainMenu(mainid);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}elseif(order.equals("
allowExit"
)){
try{
socket.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println("
退出成功"
}elseif(order.equals("
printSuccess"
System.out.println(pars[1]);
}
}
publicvoidlogin(Stringuser,Stringpassword){
this.selfName=user;
StringloginOrder="
login/"
+user+"
+password;
this.sendMessage(loginOrder);
publicvoidregister(Stringuser,Stringpassword){
Stringregiser="
register/"
this.sendMessage(regiser);
publicvoidusersOnline(){
StringusersOnline="
usersOnline/"
this.sendMessage(usersOnline);
publicvoidchat(StringtargetUser,Stringwords){
StringchatOrder="
msg/"
+this.selfName+"
+targetUser+"
+words;
this.sendMessage(chatOrder);
publicvoidchatAll(Stringwords){
StringchatAllOrder="
/AllChat/"
this.sendMessage(chatAllOrder);
publicvoidexit(){
StringexitOrder="
exit/"
+this.selfName;
this.sendMessage(exitOrder);
this.listener.flag=false;
publicstaticvoidmain(String[]args)throwsUnknownHostException,
IOException,InterruptedException{
Clientclient=newClient("
127.0.0.1"
6668);
client.printMainMenu(false);
intmainid=client.readInt();
client.processMainMenu(mainid);
publicvoidprocessMainMenu(intmainMenuID)throwsInterruptedException{
clear();
if(mainMenuID==1&
isLogin==false){
this.doLogin();
if(mainMenuID==2&
isLogin==true){
doexit();
if(mainMenuID==3){
printChatMenu();
intchatMenuID=readInt();
processChatMenu(chatMenuID);
if(mainMenuID==4){
doRegister();
if((isLogin==false)&
(mainMenuID!
=1)&
=3)&
=4)){
System.out.println("
输入错误,请重新输入"
printMainMenu(isLogin);
intmainid=readInt();
processMainMenu(mainid);
if(isLogin==true&
mainMenuID!
=2&
=3&
=4){
publicvoiddoRegister()//注册菜单
{
System.out.println("
*************注册*****************"
请你输入用户名:
Stringuser=readString();
请你输入密码:
Stringpassword=readString();
this.connetion.register(user,password);
publicvoidprintMainMenu(booleanisLogin){//主菜单
***************主菜单*****************"
if(!
isLogin){
1:
登录"
if(isLogin==true){
2:
退出"
3:
聊天"
4:
注册"
*************************************"
请按数字选择:
publicvoidprintChatMenu(){//聊天菜单
***************聊天菜单*****************"
群聊"
返回主菜单"
publicvoidprocessChatMenu(intchatMenuID)throwsInterruptedException{
if(chatMenuID==1){
***************在线用户*****************"
printAllOnlineUsers();
Thread.sleep(1000);
请输入聊天对象:
StringchatName=readString();
processChat(chatName);
if(chatMenuID==2){
processAllChat();
if(chatMenuID==3){
publicvoidprintAllOnlineUsers(){//显示所有在线用户
this.connetion.usersOnline();
publicvoidprocessChat(StringchatName){//聊天
System.out.print("
对"
+chatName+"
说:
Stringword=readString();
this.connetion.chat(chatName,word);
publicvoidprocessAllChat(){//群聊
输入消息内容:
this.connetion.chatAll(word);
publicvoiddoLogin(){//登录
***************请登录*****************"
this.connetion.login(user,password);
publicvoiddoexit(){//登录
this.connetion.exit();
publicvoidclear(){
for(inti=0;
i<
2;
i++)
System.out.println();
Scannersc=newScanner(System.in);
publicintreadInt(){
returnsc.nextInt();
publicStringreadString(){
returnsc.next();
}
服务端:
import.ServerSocket;
import.Socket;
import.UnknownHostException;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.ArrayList;
importjava.util.Hashtable;
importjava.util.Iterator;
importjava.util.Set;
publicclassServer{
privateintport;
privateHashtable<
String,ChatConnection>
map=newHashtable<
();
privateServerSocketserverSocket;
privateAccepteraccepter=newAccepter();
publicServer(intport)throwsIOException{
serverSocket=newServerSocket(port);
accepter.start();
//监听新的socket连上来的线程
publicbooleanlogin(StringuserName,Stringpassword)throwsClassNotFoundException,java.sql.SQLException{//登录
booleanb=false;
Stringsql="
select*fromchatuserswhereChat_name='
+userName+"
'
andChat_password='
+password+"
MyJDBCdb=newMyJDBC();
java.sql.ResultSetrs=db.excuteQuery(sql);
while(rs.next())
{
if(rs.getString("
Chat_name"
).equals(userName)&
rs.getString("
Chat_password"
).equals(password))
{
b=true;
}else{
b=false;
returnb;
//接受连接
publicclassAccepterextendsThread{
booleanflag=true;
publicvoidrun(){
while(flag){
try{
Socketsocket=serverSocket.accept();
ServerConnectionconn=newServerConnection(socket);
}catch(IOExceptione){
//TODOAuto-generatedca