您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 聊天系统(客户端、服务器端)-java版-完整代码
客户端:importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;publicclassChatClientextendsFrame{Sockets=null;DataOutputStreamdos=null;DataInputStreamdis=null;privatebooleanbConnected=false;TextFieldtfTxt=newTextField();TextAreataContent=newTextArea();ThreadtRecv=newThread(newRecvThread());publicstaticvoidmain(String[]args){newChatClient().launchFrame();}publicvoidlaunchFrame(){setLocation(400,300);this.setSize(300,300);add(tfTxt,BorderLayout.SOUTH);add(taContent,BorderLayout.NORTH);pack();this.addWindowListener(newWindowAdapter(){@OverridepublicvoidwindowClosing(WindowEventarg0){disconnect();System.exit(0);}});tfTxt.addActionListener(newTFListener());setVisible(true);connect();tRecv.start();}publicvoidconnect(){try{s=newSocket(127.0.0.1,8888);dos=newDataOutputStream(s.getOutputStream());dis=newDataInputStream(s.getInputStream());System.out.println(connected!);bConnected=true;}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicvoiddisconnect(){try{dos.close();dis.close();s.close();}catch(IOExceptione){e.printStackTrace();}/*try{bConnected=false;tRecv.join();}catch(InterruptedExceptione){e.printStackTrace();}finally{try{dos.close();dis.close();s.close();}catch(IOExceptione){e.printStackTrace();}}*/}privateclassTFListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){Stringstr=tfTxt.getText().trim();//taContent.setText(str);tfTxt.setText();try{//System.out.println(s);dos.writeUTF(str);dos.flush();//dos.close();}catch(IOExceptione1){e1.printStackTrace();}}}privateclassRecvThreadimplementsRunnable{publicvoidrun(){try{while(bConnected){Stringstr=dis.readUTF();//System.out.println(str);taContent.setText(taContent.getText()+str+'\n');}}catch(SocketExceptione){System.out.println(退出了,bye!);}catch(EOFExceptione){System.out.println(推出了,bye-bye!);}catch(IOExceptione){e.printStackTrace();}}}}服务器端importjava.io.*;importjava.net.*;importjava.util.*;publicclassChatServer{booleanstarted=false;ServerSocketss=null;ListClientclients=newArrayListClient();publicstaticvoidmain(String[]args){newChatServer().start();}publicvoidstart(){try{ss=newServerSocket(8888);started=true;}catch(BindExceptione){System.out.println(端口使用中....);System.out.println(请关掉相关程序并重新运行服务器!);System.exit(0);}catch(IOExceptione){e.printStackTrace();}try{while(started){Sockets=ss.accept();Clientc=newClient(s);System.out.println(aclientconnected!);newThread(c).start();clients.add(c);//dis.close();}}catch(IOExceptione){e.printStackTrace();}finally{try{ss.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}classClientimplementsRunnable{privateSockets;privateDataInputStreamdis=null;privateDataOutputStreamdos=null;privatebooleanbConnected=false;publicClient(Sockets){this.s=s;try{dis=newDataInputStream(s.getInputStream());dos=newDataOutputStream(s.getOutputStream());bConnected=true;}catch(IOExceptione){e.printStackTrace();}}publicvoidsend(Stringstr){try{dos.writeUTF(str);}catch(IOExceptione){clients.remove(this);System.out.println(对方退出了!我从List里面去掉了!);//e.printStackTrace();}}publicvoidrun(){try{while(bConnected){Stringstr=dis.readUTF();System.out.println(str);for(inti=0;iclients.size();i++){Clientc=clients.get(i);c.send(str);//System.out.println(astringsend!);}/*for(IteratorClientit=clients.iterator();it.hasNext();){Clientc=it.next();c.send(str);}*//*IteratorClientit=clients.iterator();while(it.hasNext()){Clientc=it.next();c.send(str);}*/}}catch(EOFExceptione){System.out.println(Clientclosed!);}catch(IOExceptione){e.printStackTrace();}finally{try{if(dis!=null)dis.close();if(dos!=null)dos.close();if(s!=null){s.close();//s=null;}}catch(IOExceptione1){e1.printStackTrace();}}}}}
本文标题:聊天系统(客户端、服务器端)-java版-完整代码
链接地址:https://www.777doc.com/doc-3891832 .html