《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.ShowInpulDialog(”请输入对方IP:*);Socketsocket=newSocket(sendtoIP,1234);BufferedReaderbr=newBufferedReadef(newInputStreamReader(socket,getInputStream():PrintStreamps=newPrinlStream(socket,geIOutputStream();File