java聊天小程序课程设计.docx

上传人:李司机 文档编号:7179832 上传时间:2024-06-29 格式:DOCX 页数:14 大小:21.57KB
返回 下载 相关 举报
java聊天小程序课程设计.docx_第1页
第1页 / 共14页
java聊天小程序课程设计.docx_第2页
第2页 / 共14页
java聊天小程序课程设计.docx_第3页
第3页 / 共14页
java聊天小程序课程设计.docx_第4页
第4页 / 共14页
java聊天小程序课程设计.docx_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《java聊天小程序课程设计.docx》由会员分享,可在线阅读,更多相关《java聊天小程序课程设计.docx(14页珍藏版)》请在三一办公上搜索。

1、tryif(client.socket.isConnected()(elseStringaddr=J0ptionPane.ShOWInPUtDialog(请输入服务器IP:):InelAddressHddress=InetAddress.geIByName(addr):InetSocketAddressSoCketAddreSS=newInetSockelAddress(address,5555):client.socket,connect(SoCketAddress);client,in=newDataInputStreain(client.socket,getInpulSIreani():

2、client,out=newDataOutputStream(cllent,socket.getOutpulStream();name=client.inputName.getText();client,out.wriIeUTF(姓名为+name+”的挚友风尘仆仆地上线了.”):client,listen.SetEnabled(true);client,filesend.setEnabled(true);client.sendtoall.SetEnabled(true);client,sendtoone.SeIEnabIed(IrUe);client.emote.SetEnabled(true

3、);if(!(client,thread.isAlive()client.thread=newThread(this);clienl.thread,start();catch(IOExceptionee)client,socket,close():catch(IOExceptionel)el.PrintStackTraceO;System.exit(0);Stringem=client.emote.getSeIectedItemO.toString();if(em.equals(表情”)(NHem=;elseem+=铲;if(e.getSorceO=c1lent,sendtoall)if(cl

4、ient.socket.isConnected()(name=client.inputName.getTextO;Strings=client.inputContent.getText();if(s!=null)tryClient.out.writeUTF(name+em+”对大家说:+s);client,oul.flush();client.inputContent.setText();catch(IOExceptionel)if(e.getSourceOc1ient.sendtoone)if(client.socket.isConnected()SendtoID=JOptionPane.S

5、hoWInPUtDialOg(“请输入对方ID:*):(2) Clicent.javapackagechat;importjava.awl.*;importjava.io.*:.*:importjava.awt.event.*;importjavax.swing.*;publicclassClientpublicstaticvoidmain(Stringargs)newChatClientO:classChatClientextendsFrameimplementsAction1.islener,RunnablepublicButtonlisten,connection,sendtoall,s

6、endtoone,exit,filesend:publicJComboBoxemote;publicTextFieldinputName,inputContent;publicTextAreaChatReSUIt:publicSocketsocket=null:publicDataInputStreamin-nul1;publicDataOutputStreamot=null;publicThreadthread;ChatClientOboxl.add(listen):box1.add(connection):boxl.add(fiIesend);boxl.add(exit);Boxbox2=

7、Box.CreatellorizonlalBoxO:emote=newJCoinboBoxO;emote.sel.Model(newDefau1IComboBoxMode1(newString表情,微笑甜笑惊喜嘻嘻扮酷嘿嘿傻笑,新奇,媚眼,鬼脸,沉醉羞涩生气嚷嚷发怒难过高超菜鸟问号,狂笑大哭示爱呻吟想想”);emole.SetEnabled(false);J1.abeljlintput=newJ1.abe1(输入闲聊内容”);box2.add(jlintput);box2.add(inputContent);box2.add(emote);box2.addCsendtoall);box2.ad

8、d(sendtoone);listen.addActio111.istener(this);connection.addAction1.istener(this):filesend.addAction1.istener(this);exit.addActIon1.istener(this);sendtoall.addAction1.istener(this);catch(IOExceptionel)el.PrintStackTraceO:publicvoidactiOnPerformed(ActionEvente)(ActionProcessactionProcess=newActionPro

9、cess(this):try(actionProcess.action(e);)catch(Exceptionel)(el.PrintStackTraceO;publicvoidrun()Strings=nu11;while(true)trys=in.readlTF();ChatResult.append(n+s);catch(IOExceplione)ChatResult.SetTeXt(A1与服务器断开连接”);trysocket,closeO;catch(Exceptionep)break;(3) ChatServer.javapackagechat:Stringreceive=in.r

10、eadllTF();if(receive=null)return;if(receive.ContainS(黯然卜线了)for(inti=0;ithreads,size():i+)ServerThreadst-(ServerThread)threads.elementAt(i);st.write(*+receive+”*);elseif(receive.COntains(上线了)(for(inti=0;ithreads,size():i+)(ServerThreadst=(ServerThread)threads.elementAt(i);st.WriIe(:receive):elseif(re

11、ceive.contains(监听中)(for(inti=0;ithreads,size();i+)ServerThreadst-(ServerThread)threads.elementAt(i);si.write(*+receive+*):elseif(receive.COntainS(说)if(receive.ConlainS(大家)for(inti=0;ithreads,size();i+)ServerThreadst=(ServerThread)threads.elementAt(i):st.write(:+receive);elseinttemp=receive.indexf(“*

12、):StringtempS=receive.SUbString(temp+l,tempi);inti=Integer.parselnl(tempS);ServerThreadst=(ServerThread)threads.elementAt(i);st.write(*:JreCeiVe):st=(ServerThread)threads.elementAt(getID();st.write(*:JreCeiVe);elseServerThreadst=(ServerThread)threads,ele11entt(getID();st.write(*+receive+”*);catch(Ex

13、ceptionex)threads.removeEIement(this);ex.PrintStackTraceO;try(socket,close();Jcatch(Exceptionex)ex.PrintStackTraceO;publicvoidwrite(Stringmsg)synchronized(out)tryout.writeUTF(msg):catch(Exceptionex)publicintgetIDOreturnthis.nid;publicvoidsetID(intnid)this.nid=nid;(4) flIesendClient.javapackagechat;i

14、mportjava.io.BufferedReader;importjava.io.File;importjava.io.FiIeInputStream:importjava.io.InputStreamReader;importjava.io.OulputStreani:importjava.io.PrintStream;.Socket:importjavax.swing.JOptionPane;publicclassfiIesendClient(publicvoidfilesend()throwsException(StringSendtolP=JOPliOnPane.ShowInpulD

15、ialog(”请输入对方IP:*);Socketsocket=newSocket(sendtoIP,1234);BufferedReaderbr=newBufferedReadef(newInputStreamReader(socket,getInputStream():PrintStreamps=newPrinlStream(socket,geIOutputStream();Filefile=RetFileO:ps.println(file.getName();ps.println(file.IengtIiO);Stringmsg=br.read1.ine():if(已存在.equals(m

16、sg)JOpiionPane.ShowMeSSageDialog(null,”文件已存在,请不要重复上传!”);return;longfinish1.en-1.ong.parse1.ong(msg);I?iIeInputStreamfis=newI?iIeInputStream(file);OutputStreamout=socket.getOutputStream();bytebuffer=newbyIe1024:intlen;fis.skip(finish1.en);while(len=fis.read(buffer)!=-1)out.write(buffer,0,len);fis.clo

17、se();socket,close();publicFilegetFile()throwsExceptionFilefile=null;booleanflag=false;while(flag=false)StringfiIepath=JOptionPane.ShOWlnPUtDiaIOg(“请输入要上传的路径:):file=newFiIe(filepath);if(!file,exists()JOptionPane.ShOWMeSSageDiaIOg(null,”您输入的路径不存在,请重新输入!”);flag=false;elseif(file.isDirectory()JOptionPan

18、e.ShOWMeSSageDiaIOg(null,”占不支持文件夹上传!请输入,个文件路径!”);flag=false;elseflag=true;returnfile;(5) fiIesendServer.javapackagechat;importjava.io.BufferedReader;importjava.io.File;imporIjava.io.Fi1eOuIputStream:importjava.io.IOException;importjava.io.InputStream:importjava.io.InPUtStreamReader;importjava.io.Pri

19、ntSlreain:.ServerSocket;.Socket;importjava.text.SimpleDateFoimat;importjava,uli1.Date:importjavax.swing.JOptionPane;publicclassfiIesendServer(publicvoidfiIereceiveOthrowsExceptionServerSocketServerSocket=newServerSocket(1234);JOptionPane.ShoWMeSSageDialOg(null,”服务已启动,绑定1234端口!);while(true)Socketsock

20、et=ServerSocket.accept();newfiIeServerThread(Socket).start();classfIleServerThreadextendsThreadSocketsocket:publicfileServerThreai(Socketsocket)this,socket=socket;publicvoidrunOFileOutputStreamfos=null;tryBufferedReaderbr=newBufferedReader(newInputStreamReader(socket,getInputStream();PrintStreamps=n

21、ewPrinlStream(socket,geIOutputStream();StringfiIeName-br.read1.ine();longiIe1.en=1.ong.parse1.ong(br.read1.ine();Filedir=newFile(upload);dir.11kdir();Filefile=newFile(dir,fileName);if(file.exists()&file.IengthO=fiIe1.en)(ps.println(已存在”);return;elseps.println(file.length();Stringtime=newSimpleDateFo

22、rmat(yyyy-MM-diHH:Iiirazss).format(newDate();System.out.printin(time+*+(file,exists()?*起先断点续传:”:”起先上传文件:)+file.getName();longstart=System.CurrentTimeMillisO;InpulStreamin=socket.gelInputStrea11();fos=newFiIeOutputStream(file,true):bytebuffer=newbyte1024:intlen;while(len=in.read(buffer)!=-1)(fos.writ

23、e(buffer,0,len):if(file.IengthO=fiIe1.en)break;fos.close():longend=System.CurrentTimeMillisO;time=newSimpleDateFormatC,yyyy-MM-ddIllkmin:ssz,).fonnat(newDate():System,out.print1n(time+*+*上传文件结束:+file.getName()+”,耗时:”+(end-start)+亮秒”);ps.prinlln(上传胜利”):socket,close();catch(IOExceplione)if(fos!=null)t

24、ry(fos.close();catch(IOExceptionel)el.PrintStackTraceO;运行结果:将5个Java源文件:ActionProces.java,ChatServer.java,CIienl.java、fiIesendClient.javafiIesendServer.Javao分别编译这5个JaVa源文件,或运行“javac礼java”吩咐,编译全部的源文件,然后运行连接服务器的主类,即ActionProces.java.服务器启动胜利结果分析:在进入闲聊室之前必需先启动服务器,实现服务器端的连接。然后运行ChatSerVer.java结果分析:进行服务器监听

25、,再运行CIienl.java。私聊结果分析:进入闲聊室,在输入框中输入闲聊内容,点击私牌群聊结果分析:多个用户进入闲聊室之后,在输入框中输入闲聊内容,点击群聊运行fiIesendClient.java%fiIesendServer.java文件。结果分析:进行文件传送Av设计体会这次设计经过几天的不懈努力,程序基本满意闲聊的须要。完成后的程序实现了用户登陆闲聊室、用户在线闲聊、用户退出等功能。在这次程序设计中,我主要负责需求分析,功能需求描述,系统模块图等的撰写,程序设计期间,通过询问老师,同学和网上搜寻相关解答等多种方式学习到很多课堂上没有的学问,还积累了肯定的实践阅历,增加动手实力和解决

26、实际问题的实力。在此之前,对于Java编程技术或网络socket编程学问都只是略知皮毛,缺乏深化的相识,对其工作方式和原理缺乏透彻的相识。在这段时间里,我仔细学习了Java语言开发,Applel小程序实现,对编程思想有了进步的体会,养成了些良好的编程习惯。程序虽然完成,但是距离优秀仍存在肯定差距,用JAVA开发大型项目还有很多我须要去努力学习的学问。希望自己能不断学习和实践,争取以后做得更好。限于我的学问水平和阅历有限,此程序还有很多有待完善和改正的地方,恳请老师指责指正。九、参考文献1侯俊杰.深化浅出MFCM,武汉:华中科技高校出版社,20O1.2朱福喜,傅建明,唐晓军.Java项目设计与开

27、发范例M.北京:电子工业出版社,2005.3朱福喜,唐晓军.Java程序设计技巧与开发实例M.北京:人民邮电出版社,2004.4洪维恩,何嘉.Java面对对象程序设计M.北京:中国铁道出版社,2005.5刘慈宁Java程序设计M.北京:机械工业出版社,2005.6j殷兆麟Java网络编程基础用.北京:清华高校出版社,2004.7黄强.WINDOWS网络编程IM).北京:人民邮电出版社,2003.8 W.RichardStevens.TCP/IP详解M.范建华,译.北京:机械工业出版社,2000.9 AndrewS.Tanenbaum,VrijeUniversiteit,Amsterdam,Th

28、eNetherla.计算机网络M.4版.潘爱民,译.北京:清华高校出版社,2004.10林晓基于TCP/IP的局域网闲聊工具设计J.福州:福建电脑,2007,(10):123-124.11林锐.软件工程思想IMl.西安:西安电子科技高校出版社,2001.12耿祥义,张跃平.Java2好用教程IMI.2版.北京:清华高校出版社,2006.13赵海亮.局域网闲聊程序的分析与实现J.北京:中国科技信.2004(22):17-18.14马勇.基于UDP多播文件传输方法的探讨J.福州:福建电脑.2009,(2):69-70.15黄海芳,宋筱媛,基于UDP组播的局域网闲聊设计J.福州:福建电脑.2008,(2):121722.

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号