java服务器与客户端源码1

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

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

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

资源描述

服务器(server).............................................................................................................................1类ServerTest......................................................................................................................1类ChatClient......................................................................................................................2类MyQueue.............................................................................................................................3类ReceiveFromClient.....................................................................................................4类SendToClientThread.................................................................................................11客户端(client)......................................................................................................................12类StudentWindow............................................................................................................12类StudentDuQu.................................................................................................................15类ClientTest....................................................................................................................19类BaoCunJiLu....................................................................................................................20服务器(server)类ServerTestpackageServer_System;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;importjava.util.Vector;importClient_System.TeacherWindow;publicclassServerTestextendsThread{privateintnumber;privateVectorallClient=newVector();privateMyQueuechatCon=newMyQueue();publicServerTest(intnumber){this.number=number;this.start();}publicvoidrun(){ServerSocketserver=null;try{server=newServerSocket(number);}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}newSendToClientThread(chatCon,allClient);while(true){try{Socketss=server.accept();ChatClientcc=newChatClient(ss);//System.out.println(cc.toString());allClient.add(cc);//Stringname=ss.getInetAddress().toString();chatCon.offer(cc.IP+来聊天了。。。);newReceiveFromClient(cc,chatCon,allClient);}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}类ChatClientpackageServer_System;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.PrintStream;importjava.io.Reader;importjava.net.Socket;importMysql_System.User;publicclassChatClient{publicSocketso;publicStringIP;publicPrintStreamout;publicBufferedReaderin;publicChatClient(Socketso)throwsException{this.so=so;Readerr=newInputStreamReader(so.getInputStream());in=newBufferedReader(r);OutputStreamos=so.getOutputStream();out=newPrintStream(os);IP=so.getInetAddress().toString();}//把聊天信息发到客户端publicvoidsendToClient(Stringword){out.println(word);out.flush();}//从客户端读聊天信息publicStringreadFromClient()throwsException{Stringstr=null;try{str=in.readLine();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//System.out.println(str);returnstr;}}类MyQueuepackageServer_System;importjava.util.LinkedList;publicclassMyQueue{privateLinkedListl=newLinkedList();publicbooleanoffer(Objecte){returnl.offer(e);}publicObjectpoll(){returnl.poll();}publicintsize(){returnl.size();}}类ReceiveFromClientpackageServer_System;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.util.Vector;publicclassReceiveFromClientextendsThread{MyQueuechatCon;ChatClientcc;BufferedReaderbr;VectorChatClientallClient;intnua1=0;intnua2=0;intnua3=0;publicReceiveFromClient(ChatClientcc,MyQueuechatCon,VectorallClient)throwsException{this.cc=cc;this.chatCon=chatCon;this.allClient=allClient;this.start();}publicvoidrun(){Stringline;while(true){try{line=cc.readFromClient();if(line==null){//count++;allClient.remove(cc);chatCon.offer(cc.IP+逃跑了。。。。);return;}elseif(line.equals(getAll)){inta=allClient.size();Stringrenshu=String.valueOf(a);cc.sendToClient(#/当前登录的人数:+renshu);for(ChatClientv:allClient){cc.sendToClient(IP:#+v.IP);}}elseif(line.equals(keywindow##)){for(ChatClientv:allClient){if(cc.IP.equals(v.IP)){cc.sendToClient(锁屏指令已经发出。。);}else{v.sendToClient(keywindow##);}}}elseif(line.equals(releseWindow##)){for(ChatClientv:allClient){if(cc.IP.equals(v.IP)){cc.sendToClient(解锁指令已经发出。。);}else{v.sendToClient(releseWindow##);}}}elseif(line.startsWith(SendFile##)){String[]str=line.split(##);//文件的绝对路径Stringfilename=str[1];String[]str1=filename.split(/);//文件名Stringname=str1[str1.length-1];//cc.sendToClient(SendFile##+filename);for(ChatClientv:allClient){if(cc.IP.equals(v.IP)){cc.sendToClient(文件共享指令已经发出。。);}else{v.sendToClient(SaveFile###+name);}}newSendFile(filename,cc,allClient);}//锁屏elseif(line.startsWith(Personkeywindow##)){String[]str=line.split(##IP:);Stringstr1=str[1];for(ChatClientv:allClient){if(v.IP.equals(str1)){v.sendToClient(keywindow##);}}}//解锁elseif(line.startsWith(PersonreleseWindow##)){S

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

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

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

×
保存成功