JAVA聊天室.docx

上传人:b****5 文档编号:3729595 上传时间:2022-11-25 格式:DOCX 页数:24 大小:125.81KB
下载 相关 举报
JAVA聊天室.docx_第1页
第1页 / 共24页
JAVA聊天室.docx_第2页
第2页 / 共24页
JAVA聊天室.docx_第3页
第3页 / 共24页
JAVA聊天室.docx_第4页
第4页 / 共24页
JAVA聊天室.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

JAVA聊天室.docx

《JAVA聊天室.docx》由会员分享,可在线阅读,更多相关《JAVA聊天室.docx(24页珍藏版)》请在冰豆网上搜索。

JAVA聊天室.docx

JAVA聊天室

1.Java聊天室

要求:

(1)登录聊天室(进入聊天室必须输入用户名和密码,没有用户名的用户必须申请注册。

(2)在文件中保留所有聊天记录。

(3)用户应该可以看到所有在线的其它用户,并和该用户聊天。

(4)系统应该能够提示有新用户进入和用户退出等信息。

其它功能可任意自行设计。

1、问题描述文档

对作业问题的详细描述,包括:

你要解决一个什么样的问题,这个问题给你提出了什么样的要求。

2、总体设计文档:

对你要实现的系统的总体设计,包括:

你系统的系统结构和体系框架;系统中所有对象的概要设计(如对象的主要职责和对象之间的相互关系);系统主要的控制流程;系统中重要的功能流程设计等内容。

3、源代码及详细设计文档:

源代码应该满足小组规定的源代码规范,包括命名规范,缩进对齐规范,注释规范等等。

源代码的注释应丰富并且有实用。

详细设计文档中应该对本系统中关键的问题或者你认为自己最满意的设计内容做详细的说明和归纳性总结。

4、可执行程序及其使用说明文档:

在检查时程序时可以方便的运行。

使用说明文档必须详细的介绍如何安装、运行、检查你所完成的程序。

5、人员组成及任务划分说明:

要详细说明人员的任务划分情况,同时注意,你们的源代码和设计文档中也应该有分工情况的详细记录,比如说一个类是由谁实现的,和谁完成的,谁做过什么样的修改等。

 

JAVA聊天室系统

1、问题描述

JAVA聊天室是用javaapplication程序实现的,由客户端和服务器端组成。

先启动服务器端,再启动服务器端,服务器验证身份后客户便可登陆聊天室。

客户聊天时可选择聊天对象私聊,也可以公聊。

客户登陆和退出聊天室时都会提示相关信息。

1、总体设计

界面设计:

聊天室界面大小为600*450;登陆对话框界面大小为250*200;

布局:

主要体现在客户端。

进入聊天室主界面之前有一个登陆对话框。

其中包括服务器ip,用户名和密码的填写。

聊天室的主界面主要分为两个大容器,中间是textViewTalk文本区域,显示聊天信息。

南部是一个控制界面,包括发送按钮,聊天信息输入框,在线人员组合框以及一些标签。

系统结构:

服务器端javaChatServer包括主类ChatServer和ServerThread

客户端javaChatClient包括主类ChatClient和ClientThread

主要对象及其关系

服务器端

主要类:

ChatServer//主类

ServerThread//处理客户端消息的线程类对象

主要对象:

ServerSocketserverSocket;//服务器插口对象

privateSocketsocket;//定义客户端套接字

privateBufferedReaderin;//定义输入流

privatePrintWriterout;//定义输出流

privatestaticVectoronlineUser;

privatestaticVectorsocketUser;

privateStringstrReceive,strKey;

privateStringTokenizerst;

客户端

主要类:

ChatClient//主类

ClientThread//处理聊天情境的类

ChatClient中主要对象:

JFrameframe;//窗口类对象聊天室主窗口

JDialogdialogLogin;//登陆对话框

JPanelpanelLogin;

JPanelpanelBack;

JPanelpanelTalk;

JLabellabelServerIP;

JLabellabelName;

JLabellabelPassword;

JLabellabelTalk;

JLabellabelTo;

JTextAreatextViewTalk;//显示聊天内容的文本区

JTextFieldtextTalk;

JTextFieldtextServerIP;

JTextFieldtextName;

JPasswordFieldtextPassword;

JButtonbuttonSend;

JButtonbuttonLogin;

JButtonbuttonReg;

JComboBoxlistOnline;

GridBagLayoutgl;

BorderLayoutbdl;

GridBagConstraintsgbc;

Socketsocket;//客户端插口对象

BufferedReaderin;

PrintWriterout;

StringstrSend,strReceive,strKey,strStatus;

privateStringTokenizerst;

ClientThread中主要对象

privateSocketsocket;//客户端插口对象

privateBufferedReaderin;//输入流

privatePrintWriterout;//输出流

privateStringstrReceive,strKey;

privateThreadthreadTalk;

privateStringTokenizerst;

2、源代码及详细设计

/**********************************源代码*************************************/

/*------------------------------------------

类名称:

ChatServer

程序简介:

创建一个聊天室监听服务,与客户端建立联系

--------------------------------------------*/

importjava.awt.event.*;

importjava.io.*;

import.*;

importjava.util.*;

publicclassChatServer{

ServerSocketserverSocket;//创建服务器端套接字

privatefinalintSERVER_PORT=8049;//定义端口号

publicChatServer(){

try{

serverSocket=newServerSocket(SERVER_PORT);//启动服务

System.out.println("Serverstarted...");

System.out.println("Serverportis:

"+SERVER_PORT);

getIP();//得到并显示服务器端IP

while(true){

Socketsocket=serverSocket.accept();//监听客户端的连接请求,并返回客户端socket

newServerThread(socket);//创建一个新线程来处理与该客户的通讯

}

}

catch(IOExceptione){

System.out.println("[ERROR]Coundnotstartserver."+e);

}

}

//得到服务器IP地址并显示

publicvoidgetIP(){

try{

InetAddresslocalAddress=InetAddress.getLocalHost();

byte[]ipAddress=localAddress.getAddress();

System.out.println("ServerIPis:

"+(ipAddress[0]&0xff)+"."+(ipAddress[1]&0xff)+"."+

(ipAddress[2]&0xff)+"."+(ipAddress[3]&0xff));

}

catch(Exceptione){

System.out.println("[ERROR]CoundnotgetIP."+e);

}

}

//实例化服务器端程序

publicstaticvoidmain(Stringargs[]){

newChatServer();

}

}

/*------------------------------------------

类名称:

ServerThread

程序简介:

接收到客户端socket发来的信息后进行解析、处理、转发。

--------------------------------------------*/

classServerThreadextendsThread{

privateSocketsocket;//定义客户端套接字

privateBufferedReaderin;//定义输入流

privatePrintWriterout;//定义输出流

privatestaticVectoronlineUser=newVector(10,5);

privatestaticVectorsocketUser=newVector(10,5);

privateStringstrReceive,strKey;

privateStringTokenizerst;

privatefinalStringUSERLIST_FILE="D:

\\Java大作业\\聊天室\\user.txt";//设定存放用户信息的文件

publicServerThread(Socketclient)throwsIOException{

socket=client;

in=newBufferedReader(newInputStreamReader(socket.getInputStream()));//客户端接收

out=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())),true);//客户端输出

this.start();

}

publicvoidrun(){

try{while(true){

strReceive=in.readLine();//从服务器端接收一条信息后拆分、解析,并执行相应操作

st=newStringTokenizer(strReceive,"|");

strKey=st.nextToken();

if(strKey.equals("login")){

login();

}

elseif(strKey.equals("talk")){

talk();

}

elseif(strKey.equals("init")){

initClientOnline();

}

elseif(strKey.equals("reg")){

register();

}

}}

catch(IOExceptione){//用户关闭客户端造成此异常,关闭该用户套接字。

StringleaveUser=closeSocket();

System.out.println("[SYSTEM]"+leaveUser+"leavechatroom!

");

sendAll("talk|>>>"+leaveUser+"恋恋不舍的离开了聊天室。

");

}

}

//判断是否有该注册用户

privatebooleanisExistUser(Stringname){

StringstrRead;

try{

FileInputStreaminputfile=newFileInputStream(USERLIST_FILE);//createinputstream

DataInputStreaminputdata=newDataInputStream(inputfile);

while((strRead=inputdata.readLine())!

=null){

StringTokenizerstUser=newStringTokenizer(strRead,"|");

if(stUser.nextToken().equals(name)){

returntrue;

}

}

}

catch(FileNotFoundExceptionfn){

System.out.println("[ERROR]UserFilehasnotexist!

"+fn);

out.println("warning|读写文件时出错!

");

}

catch(IOExceptionie){

System.out.println("[ERROR]"+ie);

out.println("warning|读写文件时出错!

");

}

returnfalse;

}

//判断用户的用户名密码是否正确

privatebooleanisUserLogin(Stringname,Stringpassword){

StringstrRead;

try{

FileInputStreaminputfile=newFileInputStream(USERLIST_FILE);

//createinputstream

DataInputStreaminputdata=newDataInputStream(inputfile);

while((strRead=inputdata.readLine())!

=null){

if(strRead.equals(name+"|"+password)){

returntrue;

}

}

}

catch(FileNotFoundExceptionfn){

System.out.println("[ERROR]UserFilehasnotexist!

"+fn);

out.println("warning|读写文件时出错!

");

}

catch(IOExceptionie){

System.out.println("[ERROR]"+ie);

out.println("warning|读写文件时出错!

");

}

returnfalse;

}

//用户注册

privatevoidregister()throwsIOException{

Stringname=st.nextToken();//得到用户名称

Stringpassword=st.nextToken().trim();//得到用户密码

if(isExistUser(name)){

System.out.println("[ERROR]"+name+"Registerfail!

");

out.println("warning|该用户已存在,请改名!

");

}

else{

RandomAccessFileuserFile=newRandomAccessFile(USERLIST_FILE,"rw");

userFile.seek(userFile.length());//在文件尾部加入新用户信息

userFile.writeBytes(name+"|"+password+"\r\n");

longin(name);//自动登陆聊天室

}

}

//用户登陆(从登陆框直接登陆)

privatevoidlogin()throwsIOException{

Stringname=st.nextToken();//得到用户名称

Stringpassword=st.nextToken().trim();//得到用户密码

booleansucceed=false;

System.out.println("[USERLOGIN]"+name+":

"+password+":

"+socket);

for(inti=0;i

if(onlineUser.elementAt(i).equals(name)){

System.out.println("[ERROR]"+name+"islogined!

");

out.println("warning|"+name+"已经登陆聊天室");

}

}

if(isUserLogin(name,password)){//判断用户名和密码

longin(name);

succeed=true;

}

if(!

succeed){

out.println("warning|"+name+"登陆失败,请检查您的输入!

");

System.out.println("[SYSTEM]"+name+"loginfail!

");

}

}

//用户登陆

privatevoidlongin(Stringname)throwsIOException{

out.println("login|succeed");

sendAll("online|"+name);

onlineUser.addElement(name);

socketUser.addElement(socket);

sendAll("talk|>>>欢迎"+name+"进来与我们一起交谈!

");

System.out.println("[SYSTEM]"+name+"loginsucceed!

");

}

//聊天信息处理

privatevoidtalk()throwsIOException{

StringstrTalkInfo=st.nextToken();//得到聊天内容;

StringstrSender=st.nextToken();//得到发消息人

StringstrReceiver=st.nextToken();//得到接收人

System.out.println("[TALK_"+strReceiver+"]"+strTalkInfo);

SocketsocketSend;

PrintWriteroutSend;

//得到当前时间

GregorianCalendarcalendar=newGregorianCalendar();

StringstrTime="("+calendar.get(Calendar.HOUR)+":

"+calendar.get(Calendar.MINUTE)+":

"+calendar.get(Calendar.SECOND)+")";

strTalkInfo+=strTime;

if(strReceiver.equals("All")){

sendAll("talk|"+strSender+"对所有人说:

"+strTalkInfo);

}

else{

if(strSender.equals(strReceiver)){

out.println("talk|>>>不能自言自语哦!

");

}

else{

for(inti=0;i

if(strReceiver.equals(onlineUser.elementAt(i))){

socketSend=(Socket)socketUser.elementAt(i);

outSend=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socketSend.getOutputStream())),true);

outSend.println("talk|"+strSender+"对你说:

"+strTalkInfo);

}

elseif(strSender.equals(onlineUser.elementAt(i))){

socketSend=(Socket)socketUser.elementAt(i);

outSend=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socketSend.getOutputStream())),true);

outSend.println("talk|你对"+strReceiver+"说:

"+strTalkInfo);

}

}

}

}

}

//初始化在线用户列表

privatevoidinitClientOnline()throwsIOException{

StringstrOnline="online";

for(inti=0;i

strOnline+="|"+onlineUser.elementAt(i);

}

out.println(strOnline);

}

//信息群发

privatevoidsendAll(StringstrSend){

SocketsocketSend;

PrintWriteroutSend;

try{

for(inti=0;i

socketSend=(Socket)socketUser.elementAt(i);

outSend=newPrintWriter(newBufferedWriter(newOutp

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1