SVN服务器框架搭建和代码填充(SVN简单版)
2016-04-12 12:39
204 查看
SVN服务器框架搭建和代码填充(SVN简单版)
今天早上我把服务器SVN服务器的框架搭建好了,代码也填充进去了,但是没有编写客户端进行交互测试,因为SVN服务器代码的编写是我的工作,而客户端的代码是另一个队员的工作,理论上说我的工作已经完成了,剩下的就是跟他的客户端交接,实现SVN的完整功能。SVN服务器端主要实现的功能是:上传和下载文件、验证用户名密码和提供注册。
1.服务器框架:创建服务器对象,创建客户端线程
代码如下:package control; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import dao.FileAndSocketList; public class MyServer { public static void main(String[] args) { MyServer ms = new MyServer(); ms.initMyServer(); } public void initMyServer(){ try { ServerSocket ss = new ServerSocket(); //创建一个服务器对象 while(true){ Socket client = ss.accept(); System.out.println("有客户端连接上服务器..."); SocketThread st = new SocketThread(client); //创建客户端线程,让服务器可以对多个客户端操作 FileAndSocketList.socketList.add(st); //添加客户端线程到客户端列表,用于群体更新文件列表 st.start(); } } catch (IOException e) { e.printStackTrace(); } } }
2.对于每一个客户端线程,创建一种协议。
检测其发送到服务器的字符串,一一对应客户端请求的服务,服务器的主要流程一共有5个:登录验证、注册请求、上传文件、下载文件和获取服务器列表。package control; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.net.Socket; import common.Tool; import dao.UserData; public class SocketThread extends Thread{ public Socket client; public InputStream ins; public OutputStream ous; public ObjectInputStream ois; public ObjectOutputStream oos; public String userName; public SocketThread(Socket client){ this.client = client; } public void run() { try { userName = new String(); ins = client.getInputStream(); //获取输入输出流 ous = client.getOutputStream(); ois = new ObjectInputStream(ins); //包装成对象流 oos = new ObjectOutputStream(ous); String value = Tool.receiveMsg(ois); while(true){ if( value.equals("Login") ){ CheckAndCreat.checkUser(ois, oos, this); }else if( value.equals("CreatUser") ){ CheckAndCreat.creatUser(ois, oos); }else if( value.equals("DownLoad") ){ DownOrUpLoad.DownLoad(oos, ois); }else if( value.equals("UpLoad") ){ DownOrUpLoad.UpLoad(ois, this); }else if( value.equals("UpdateFileList") ){ DownOrUpLoad.SendFileMsgToCustom(oos); } value = Tool.receiveMsg(ois); } } catch (Exception e) { e.printStackTrace(); } } }
3.5个主要流程分5个方法执行程序
CheckAndCreat类有验证登录和注册用户的方法package control; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import common.Tool; import dao.UserData; public class CheckAndCreat { public static boolean checkNamePassword(String userName,String passWord){ if( UserData.userData.containsKey(userName) && UserData.userData.get(userName) == passWord ){ return true;//如果用户数据里面有该用户名并且用户密码也匹配,返回true } return false; } public static void checkUser(ObjectInputStream ois,ObjectOutputStream oos,SocketThread st){ int count = 3; try{ String userName = Tool.receiveMsg(ois); String passWord = Tool.receiveMsg(ois); while( count >= 0 ){ if( checkNamePassword(userName, passWord) ){//验证成功,发送LoginSucceed String Msg = "LoginSucceed\r\n"; Tool.Send(oos, Msg); st.userName = userName; return ; }else if( count > 0 ){//验证失败,累计次数,只能验证三次 String Msg = "Fail!Please input again!\r\n"; Tool.Send(oos, Msg); userName = Tool.receiveMsg(ois); passWord = Tool.receiveMsg(ois); count -- ; } } }catch(Exception e){ } } public static void creatUser(ObjectInputStream ois,ObjectOutputStream oos){ try{ String userName = Tool.receiveMsg(ois); String passWord = Tool.receiveMsg(ois); int count = 3; while( count >= 0 ){ if( !UserData.userData.containsKey(userName) ){//判断用户名时候有重名,没有重名就注册成功 UserData.userData.put(userName, passWord); String Msg = "CreatUserSucceed\r\n"; Tool.Send(oos, Msg); return ; }else if( count > 0 ){//注册失败,有重名 String Msg = "UserNameRepeat\r\n"; Tool.Send(oos, Msg); userName = Tool.receiveMsg(ois); passWord = Tool.receiveMsg(ois); count -- ; } } }catch(Exception e){ } } }下面DownOrUpLoad类提供下载和上传文件以及获取服务器文件列表的方法:
package control; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import common.Tool; import dao.FileAndSocketList; import dao.FileInfomation; public class DownOrUpLoad { public ObjectInputStream ois; public ObjectOutputStream oos; public static void DownLoad(ObjectOutputStream oos,ObjectInputStream ois){//服务器发送文件给客户端 try { String fileName = Tool.receiveMsg(ois);//客户端选中文件发送文件名给服务器,服务器匹配文件,发送文件 FileInfomation fifd = FileAndSocketList.FileInfomationmap.get(fileName); File file = new File(fifd.path);//获取服务器本地文件地址 FileInputStream fis = new FileInputStream(file);//创建文件输入流,让文件输入服务器 long count = fifd.size/2048; int last = (int) (fifd.size%2048); oos.writeLong(count); oos.writeInt(last); oos.flush(); byte[] bytes = new byte[2048]; int value = fis.read(bytes); while( count > 0 ){ oos.write(bytes); oos.flush(); value = fis.read(bytes); count -- ; } if( last > 0 ){ bytes = new byte[2048]; fis.read(bytes); oos.write(bytes); oos.flush(); } oos.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } public static void UpLoad(ObjectInputStream ois,SocketThread st){//服务器接收客户端上传文件 try{ String fileName = Tool.receiveMsg(ois); long size = ois.readLong(); String path = "C:\\Users\\asus\\Desktop\\javaData\\SVN项目数据\\SVN服务器"; String owner = st.userName; FileInfomation fifd = new FileInfomation(fileName,size,path,owner); File file = new File(path); FileOutputStream fos = new FileOutputStream(file); long count = ois.readLong(); int last = ois.readInt(); byte[] bytes = new byte[2048]; while( count > 0 ){ ois.readFully(bytes); fos.write(bytes); fos.flush(); count -- ; } if( last > 0 ){ bytes = new byte[2048]; ois.readFully(bytes); fos.write(bytes); fos.flush(); } fos.close(); ois.close(); FileAndSocketList.FileInfomationmap.put(fileName, fifd); }catch(Exception e){ } } public static void SendFileMsgToCustom(ObjectOutputStream oos){//服务器发送文件列表给客户端 HashMap<String,FileInfomation> newMap = new HashMap<String,FileInfomation>(); newMap.putAll(FileAndSocketList.FileInfomationmap); try { oos.writeObject(newMap); } catch (Exception e) { e.printStackTrace(); } } }
这就是我的简单版本的SVN服务器框架,但只是进行了代码的编写,没有做交接测试。
相关文章推荐
- C++ vector 数组实现
- C++ String类 ( 构造、拷贝构造、赋值运算符重载和析构函数)
- 编程知识汇总--3D模型文件的通用格式:FBX
- wxpython ImportError: DLL load failed: 找不到指定的模块
- Java源码解析-hashmap
- C/C++——strcpy函数的实现 和解析
- maven导入项目时把jdk变成1.8的解决办法
- 发光shader代码
- MATLAB的fft2和OpeCV的cvDFT的结果是一样的哦!但为什么有时候会相差255倍呢?
- 【C语言】取值符&和间接值*辨析
- 《统计学习方法》-KNN笔记和python源码
- eclipse下android项目解决方法数id超过65535
- 工具相关——用Eclipse的SVN插件管理一般目录
- Qt实现自定义按钮的三态效果
- Qt4.8.2 QPushButton按钮贴图以及实现按钮的几种常用状态
- C++ 和Matlab混合编程一些问题:如何让matlab不打开新的command windows窗口
- [C#] 静态方式实现的单例是不是懒汉模式?
- 可视化——matploblib 解决中文显示的问题
- C++ string与stringstream
- java 堆 栈 方法区的简单分析