《java服务器与客户端源码.doc》由会员分享,可在线阅读,更多相关《java服务器与客户端源码.doc(21页珍藏版)》请在金锄头文库上搜索。
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;import Client_S
2、ystem.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 = new ServerSocket(n
3、umber); 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.getInetAddress().toString()
4、; 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.io.OutputStream
5、;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 InputStreamReader(so.getInputSt
6、ream(); 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 String str = null;try s
7、tr = 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(Object e) return l.offer(e);
8、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 extends ThreadMyQueue chatCon
9、;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() String line ; while(true)
10、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);else if(line.equals(keywi
11、ndow#)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.startsWith(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#