JAVA局域网聊天系统源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

这是我自己做的简单聊天系统客户端packageLiaoTianSys;importjava.awt.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.Socket;importjava.net.UnknownHostException;publicclassConversationFrameextendsJFrame{JScrollPanejsp;JTextFieldjtf;staticJTextAreajta;//JTextAreajat1,jta2;JButtonenter=newJButton(发送);JButtonjb=newJButton(聊天室好友);JButtonjb2=newJButton(进入聊天室);JButtonjb3=newJButton(刷新在线人员列表);JPaneljp,jp1,jp3,jp4;DefaultListModellistmodel=newDefaultListModel();//staticString[]NAME=newString[10];Stringn[]={f};JListlist=newJList(listmodel);JLabeltime=newJLabel(当前系统时间:);JLabelshowtime=newJLabel(显示时间);JLabeljl=newJLabel(输聊天信息);JLabelnicheng=newJLabel(昵称);JTextFieldNCshuru=newJTextField(10);staticDataOutputStreamdos;staticDataInputStreamdis;//finalLoginFramelf;Socketsocket;publicConversationFrame(){Containercon=getContentPane();con.setLayout(newBorderLayout());jp=newJPanel();setSize(700,600);setLocation(100,100);jta=newJTextArea();jta.setEditable(false);jsp=newJScrollPane(jta);con.add(jsp,BorderLayout.CENTER);jtf=newJTextField(20);jp.add(jl);jp.add(jtf);jp.add(enter);con.add(jp,BorderLayout.SOUTH);jp1=newJPanel(newBorderLayout());JScrollPanejsp1=newJScrollPane(list);jp1.add(jsp1,BorderLayout.CENTER);jp1.add(jb,BorderLayout.NORTH);con.add(jp1,BorderLayout.EAST);//pack();jp3=newJPanel();jp3.add(nicheng);jp3.add(NCshuru);jp3.add(jb2);con.add(jp3,BorderLayout.NORTH);jp4=newJPanel(newGridLayout(10,1));jp4.add(jb3);jp4.add(time);jp4.add(showtime);newgetTime(this).start();con.add(jp4,BorderLayout.WEST);setVisible(true);//发送信息给所有人enter.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){//TODOAuto-generatedmethodstubStringinfo=jtf.getText();try{dos.writeUTF(NCshuru.getText()+对所有人说:+info);dos.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}});//进入聊天室时将自己的昵称发给服务器,首先去验证是否会与已有的人同名,本聊天室是不支持同昵称聊天jb2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Stringmsg=NCshuru.getText().toString();//得到昵称if(msg.length()==0){JOptionPane.showMessageDialog(null,昵称不应该为空,,温馨提示,JOptionPane.INFORMATION_MESSAGE);}{try{dos.writeUTF(name+msg);dos.flush();}catch(Exceptionex){System.out.println(ex.getMessage());}}}});//向服务器请求更新在线人员列表jb3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){try{listmodel.removeAllElements();//请求之前删除JList对象中的所有内容dos.writeUTF(请求更新在线人员列表);dos.flush();}catch(IOExceptione1){//TODOAuto-generatedcatchblock//e1.printStackTrace();System.out.println(e1.getMessage());}}});//当有在线人员时,双击JList列表某项弹出私聊对话框事件,匿名内部类实现的list.addMouseListener(newMouseAdapter(){publicvoidmouseClicked(MouseEvente){if(e.getClickCount()==2){intindex=list.locationToIndex(e.getPoint());//获得list表中的索引值Stringrecevier=(String)listmodel.getElementAt(index);//得到索引对应的值Stringsender=NCshuru.getText();newDanliao(sender,recevier,socket);}}});}publicstaticvoidmain(String[]args){ConversationFrameCon=newConversationFrame();Con.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Con.kaishi();}publicvoidkaishi(){try{socket=newSocket(172.16.14.60,8888);dos=newDataOutputStream(socket.getOutputStream());dis=newDataInputStream(socket.getInputStream());pcpc1=newpc(socket,this);//传送套接字,本类对象给pc线程pc1.start();}catch(UnknownHostExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}//pc线程类,负责接收服务器发送来的信息classpcextendsThread{ConversationFrameconver;Socketsocket;publicpc(Socketsocket,ConversationFrameconver){this.conver=conver;this.socket=socket;}publicvoidrun(){try{inti=0;while(true){Stringline;//从线路读取信息line=ConversationFrame.dis.readUTF();//将所有的在线人员昵称发送给JList,并添加到在线人列表中if(line.startsWith(N))//服务器发送过来的信息的格式是N+c.name{Stringna=line.substring(1);try{conver.listmodel.addElement(na);System.out.println(na);}catch(Exceptione){System.out.println(e.getMessage());}}//接受服务器发来的广播聊天信息else{ConversationFrame.jta.append(line+\n);}}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();System.out.println(e.getMessage());}}}服务器端packageLiaoTianSys;importjava.io.*;importjava.net.*;importjava.util.*;publicclassServerimplementsRunnable{publicstaticfinalintport=8888;protectedServerSocketss;staticVectorconnections;Threadconnect;publicServer(){try{ss=newServerSocket(port);connections=newVector(1000);connect=newThread(this);connect.start();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticvoidmain(String[]args){newServer();}publicvoidrun(){try{while(true){Socketclient=ss.accept();System.out.println(客户端连接上);firstthreadf=newfirstthread(this,client);f.setPriority(Thread.MIN_PRIORITY);f.start();connections.addElement(f);}}catch(IOExceptione){System.out.println(e.getMessage());}}//向所有人发送聊天信息publicvoidSenAll(Stringmsg){inti;firstthreadft;for(i=0;iconnections.size();i++){ft=(firstthread)connections.elementAt(i);try{ft.out.writeUTF(msg);}catch(Exceptione){System.out.println(e.getMessage());}}}//发送单聊信息的方法publicvoidSenOne(Stringmsg){inti;firstthreadft;/*Strings1,s2,s3;

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功