java服务器与客户端源码.doc

上传人:自*** 文档编号:126205487 上传时间:2020-03-23 格式:DOC 页数:21 大小:70KB
返回 下载 相关 举报
java服务器与客户端源码.doc_第1页
第1页 / 共21页
java服务器与客户端源码.doc_第2页
第2页 / 共21页
java服务器与客户端源码.doc_第3页
第3页 / 共21页
java服务器与客户端源码.doc_第4页
第4页 / 共21页
java服务器与客户端源码.doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《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#

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号