版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、服务器(server)1类ServerTest1类ChatClient2类MyQueue3类ReceiveFromClient4类SendToClientThread11客户端(client)12类StudentWindow12类StudentDuQu15类ClientTest19类BaoCunJiLu20服务器(server)类ServerTestpackage Server_System;import java.io.IOException;import .ServerSocket;import .Socket;import java.util.Vector
2、;import Client_System.TeacherWindow;public class ServerTest extends Thread private int number; private Vector allClient = new Vector(); private MyQueue chatCon = new MyQueue(); public ServerTest(int number) this.number=number; this.start(); public void run() ServerSocket server =null;try server = ne
3、w ServerSocket(number); catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace();new SendToClientThread(chatCon,allClient); while(true) try Socket ss=server.accept(); ChatClient cc=new ChatClient(ss);/System.out.println(cc.toString(); allClient.add(cc);/String name=ss.getInetAddr
4、ess().toString(); chatCon.offer(cc.IP+来聊天了。); new ReceiveFromClient(cc,chatCon,allClient ); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); 类ChatClientpackage Server_System;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java
5、.io.OutputStream;import java.io.PrintStream;import java.io.Reader;import .Socket;import Mysql_System.User;public class ChatClient public Socket so;public String IP;public PrintStream out;public BufferedReader in;public ChatClient(Socket so) throws Exception this.so=so; Reader r = new InputSt
6、reamReader(so.getInputStream(); in= new BufferedReader(r); OutputStream os = so.getOutputStream(); out = new PrintStream(os); IP= so.getInetAddress().toString();/把聊天信息发到客户端 public void sendToClient(String word) out.println(word);out.flush(); /从客户端读聊天信息 public String readFromClient() throws Exception
7、 String str = null;try str = in.readLine(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); /System.out.println(str); return str; 类MyQueuepackage Server_System;import java.util.LinkedList;public class MyQueue private LinkedList l=new LinkedList(); public boolean offer(Objec
8、t e) return l.offer(e); public Object poll() return l.poll(); public int size() return l.size(); 类ReceiveFromClientpackage Server_System;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.Vector;public class ReceiveFromClient exten
9、ds ThreadMyQueue chatCon;ChatClient cc;BufferedReader br;Vector allClient; int nua1 = 0; int nua2 = 0; int nua3 = 0; public ReceiveFromClient(ChatClient cc,MyQueue chatCon,Vector allClient ) throws Exception this.cc=cc;this.chatCon=chatCon;this.allClient=allClient; this.start(); public void run() St
10、ring line ; while(true) try line=cc.readFromClient();if(line=null)/count+;allClient.remove(cc);chatCon.offer(cc.IP+逃跑了。);return;else if(line.equals(getAll)int a=allClient.size();String renshu=String.valueOf(a);cc.sendToClient(#/当前登录的人数:+renshu);for(ChatClient v:allClient)cc.sendToClient(IP:#+v.IP);e
11、lse if(line.equals(keywindow#)for(ChatClient v:allClient)if(cc.IP.equals(v.IP)cc.sendToClient(锁屏指令已经发出。);elsev.sendToClient(keywindow#);else if(line.equals(releseWindow#)for(ChatClient v:allClient)if(cc.IP.equals(v.IP)cc.sendToClient(解锁指令已经发出。);elsev.sendToClient(releseWindow#);else if(line.startsWi
12、th(SendFile#)String str = line.split(#);/文件的绝对路径String filename = str1;String str1 = filename.split(/);/文件名String name =str1str1.length-1;/cc.sendToClient(SendFile#+filename);for(ChatClient v:allClient)if(cc.IP.equals(v.IP)cc.sendToClient(文件共享指令已经发出。);elsev.sendToClient(SaveFile#+name);new SendFile(
13、filename,cc,allClient);/锁屏else if(line.startsWith(Personkeywindow#)String str = line.split(#IP:);String str1 = str1;for(ChatClient v:allClient)if(v.IP.equals(str1)v.sendToClient(keywindow#);/解锁else if(line.startsWith(PersonreleseWindow#)String str = line.split(#IP:);String str1 = str1;for(ChatClient
14、 v:allClient)if(v.IP.equals(str1)v.sendToClient(releseWindow#);/监控全部人员的窗口else if(line.equals(wo#yaojian#kongAll)String str2 = cc.IP;for(ChatClient v:allClient)if(cc.IP.equals(v.IP)cc.sendToClient(监控所有人的指令已经发出。);elsev.sendToClient(wo#yaojian#kongImage+str2);/监控某个人的窗口else if(line.startsWith(wo#yaojian
15、#kongOne)String str = line.split(OneIP:);String str1 = str1;String str2 = cc.IP;for(ChatClient v:allClient)if(v.IP.equals(str1)v.sendToClient(wo#yaojian#kongImage+str2);/投票的操作else if(line.equals(Teacher#liaojie#StudenT)for(ChatClient v:allClient)if(cc.IP.equals(v.IP)cc.sendToClient(投票系统的指令已经发出。);els
16、ev.sendToClient(Teacher#liaojie#StudenT+cc.IP);/投票系统else if(line.startsWith(Student#laiojie#)int count = allClient.size()-1;String sr = line.split(#laiojie#);String sr1 = sr1;String sr2 =sr1.split(#); String IP = sr20;String xinxi = sr21;String str = null;if(xinxi.equals(budong)nua1+;str = 不懂;if(xin
17、xi.equals(youdiandong)nua2+;str = 有点懂;if(xinxi.equals(wanquandong)nua3+;str = 完全懂;int nua = nua1+nua2+nua3;if(nua=count)chatCon.offer(投票的结果:+不懂的人数:+nua1+人;+有点懂的人数:+nua2+人;+完全懂的人数:+nua3+人。); nua1=0; nua2=0; nua3=0;for(ChatClient v:allClient)if(v.IP.equals(IP)v.sendToClient(#lai#3Taxi#2wgt#+cc.IP+说:+s
18、tr);/私人聊天信息的接受else if(line.startsWith(#singleone#IP:)String str2;String str = line.split(#Person#);String str1 = str0;if(str.length=1)str2 = null;elsestr2 = str1;String str3 = str1.split(one#IP:);String str4 = str31;for(ChatClient v:allClient)if(v.IP.equals(str4)v.sendToClient(cc.IP+对你的悄悄话:+str2);cc
19、.sendToClient(你悄悄对+cc.IP+说:+str2);elsechatCon.offer(cc.IP + 说:+line);/System.out.println(who+说:+line); catch (Exception e) / TODO Auto-generated catch blockallClient.remove(cc);chatCon.offer(cc.IP+逃跑了。); 类SendToClientThreadpackage Server_System;import java.util.Vector;public class SendToClientThread
20、 extends Threadpublic static MyQueue chatCon;public static Vector allClient; public SendToClientThread(MyQueue chatCon,Vector allClient) this.chatCon=chatCon; this.allClient=allClient; this.start(); public void run() System.out.println(发送线程启动。); while(true) if(chatCon.size()=0) try Thread.sleep(300)
21、; catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace(); else String word=(String)chatCon.poll(); for(ChatClient aa:allClient) aa.sendToClient(word); 客户端(client)类StudentWindowpackage Client_System;public StudentWindow(String username)super(username+同学,欢迎进入此界面);jp.setLay
22、out(new FlowLayout(FlowLayout.CENTER,8,1);jp.add(ip);jp.add(ipt);jp.add(duankou);jp.add(duankout);jp.add(lianjie);/menu1.add(Item1);menu1.add(Item2);menu2.add(Item3);menu2.add(Item4);/menuBar.add(menu1);menuBar.add(menu2);menuBar.add(jb1);menuBar.add(jb2);menuBar.add(jb3);/this.setJMenuBar(menuBar);
23、jp1.setLayout(new BorderLayout(5, 20);jp2.setLayout(new FlowLayout(FlowLayout.CENTER,16,1);/jp2.add(jb1);jp2.add(jb2);jp2.add(jb3);jp1.add(jp2,North);jp3.setLayout(new BorderLayout(5, 20);jta.setFont(f);jta.setEditable(false);jtta.setFont(f1);jp3.add(jsp1,Center);jp3.add(jtta,South);jp1.add(jp3,Cent
24、er);jp8.setLayout(new FlowLayout(FlowLayout.CENTER,16,1);jp8.add(jb4);jp8.add(jb5);jp1.add(jp8,South);jp4.setLayout(new BorderLayout(5, 10);jp5.setLayout(new GridLayout(3,1);jp5.add(jb);jp5.add(jl);jp5.add(jl1);jp4.add(jp5,North);jp4.add(jsp3,Center);jp6.setLayout(new FlowLayout(FlowLayout.CENTER,8,
25、1);jp6.add(jb6);jp4.add(jp6,South);this.add(jp,North);this.add(jp1,Center);this.add(jp4,East);this.setSize(600,600);this.setResizable(false); this.setLocation(400, 200);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lianjie.addActionListener(new ActionListener() Overridep
26、ublic void actionPerformed(ActionEvent e) String str1 = ipt.getText();String str2 = duankout.getText();if(!str1.equals() & !str2.equals()Integer num = new Integer(str2);try so=new Socket(str1,num);new StudentDuQu(so,jta); catch (Exception e1) jta.append(你输入的服务器IP地址或端口不存在!+rn);); jb5.addActionListene
27、r(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) String str = jtta.getText();/System.out.println(str);new ClientTest(str,so);jtta.setText();); jb.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) l.removeAll();String srt = getAll;new C
28、lientTest(srt,so);); jb4.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) String text = jta.getText();System.out.println(text);JFrame jf1 = new JFrame(保存文件);FileDialog gd = new FileDialog(jf1,保存文件,FileDialog.SAVE);/jf1.setSize(400,400);gd.setVisible(true);Str
29、ing filename = gd.getDirectory()+gd.getFile();new BaoCunJiLu(text,filename);); /私人聊天系统 jb6.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) String str1 = l.getSelectedItem();String str2 = jtta.getText();String str3 = #singleone#+str1+#Person#+str2;new ClientT
30、est(str3,so);jtta.setText(););类StudentDuQuimport javax.swing.JTextArea;public class StudentDuQu extends Thread int i=0; private Socket so; private InputStream in; private BufferedReader bf; private JTextArea jt; String line =null; StudentWindow tw; String name; /JFrame jf = new JFrame(); JDialog jd
31、= new JDialog(); public StudentDuQu(Socket so,JTextArea jt) throws IOException this.so=so; this.jt=jt; in=so.getInputStream(); Reader r=new InputStreamReader(in); bf=new BufferedReader(r); this.start(); public void run() jt.append(从服务器端读取的线程已经成功起动+rn); jd.setUndecorated(true); while(true) try line =
32、 bf.readLine(); if(line=null) jt.append(警告:服务器已经关闭!); break; else if(line.startsWith(#/) String str = line.split(/); tw.jl.setText(str1); else if(line.startsWith(IP:#) String str = line.split(#); tw.l.add(str0+str1); else if(line.equals(keywindow#) boolean falg = true; new SuoPing(jd,falg); else if(
33、line.equals(releseWindow#) new SuoPing(jd,false); /文件的共享 else if(line.startsWith(SaveFile) if(line.startsWith(SaveFile#) String s1 = line.split(#); name = s11; if(line.startsWith(SaveFile#H#H#) String s2 = /home/soft01/+name; String s3 = line.split(#H#H#); String s4 = s31+rn; OutputStream out = new
34、FileOutputStream(s2,true); byte b = s4.getBytes(); out.write(b); out.close(); else if(line.startsWith(wo#yaojian#kongImage) Robot r = new Robot();/用于自动化测试的程序类/表示矩形 长1024,宽768Rectangle ret = new Rectangle(1024,768);/createScreenCapture抓 取自己的桌面图片,大小为rec指定的矩形的大小BufferedImage img = r.createScreenCapture
35、(ret);/将截图写入到某一文件中File fa = new File(/home/soft01/+i+.gif);i+;ImageIO.write(img, gif, fa); String filename = fa.getAbsolutePath(); System.out.println(line); String str = line.split(Image/); String IP = str1; new FaSongImage(filename,IP); else if(line.startsWith(Teacher#liaojie#StudenT) String s1 = l
36、ine.split(#StudenT); final String s2 = s11; final JFrame jf = new JFrame(老师发来消息); JButton jb1 = new JButton(不懂); JButton jb2 = new JButton(有点懂); JButton jb3 = new JButton(完全懂了); JLabel jl1 = new JLabel(信息:,JLabel.CENTER);jl1.setForeground(Color.red); JLabel jl2 = new JLabel(老师问你刚才讲的你懂了没?(点击按钮),JLabe
37、l.CENTER);jl2.setForeground(Color.blue); JPanel jp1 = new JPanel(); jf.setLayout(new GridLayout(3,1,0,10); jf.add(jl1);jf.add(jl2); jp1.setLayout(new FlowLayout(FlowLayout.CENTER,10,1); jp1.add(jb1);jp1.add(jb2);jp1.add(jb3); jf.add(jp1); jf.setSize(300,300); jf.setResizable(false); jf.setLocation(4
38、00, 300); jf.setDefaultCloseOperation(jf.HIDE_ON_CLOSE); jf.setVisible(true); jb1.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) String str1 = Student#laiojie#+s2+#+budong;new ClientTest(str1,so);jf.setVisible(false);); jb2.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) String str1 = Student
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 交通安全责任合同
- 养老院采购管理制度
- 废钢采购精细化管理制度
- 小学采购内控管理制度
- 江油采购制度
- 劳动防护采购使用制度
- 上海市世界外国语中学2025-2026学年九年级下学期3月学情自测语文试题 (含答案)
- 2025年圣诞节的活动策划总结(2篇)
- 2025 奇妙的水的毛细现象应用实验作文课件
- 数字化转型下DW公司农机生产车间看板管理系统的创新与实践
- (必刷)肿瘤放射治疗技术中级职称考点精粹必做300题-含答案
- ICU患者气道管理规范化方案
- 2025年应急避难场所管理协议
- 铁路客运规章应用课件 4-3 携带禁限品业务处理
- 药事管理之药品召回
- 国家义务教育质量监测组织工作手册
- 2025 外贸流程入门课件
- 开一个酒馆的策划方案
- 建设项目水资源论证报告技术审查要点(湖南省)
- 护理考编制考试题库及答案
- DB3301-T 1100-2024 古树名木健康诊断技术规程
评论
0/150
提交评论