专业: 学号: 学生姓名(签名):
设计题目:利用Socket网络编程机制实现FTP服务器
一、设计实验条件
1208实验室
二、设计任务及要求
1. 利用Socket网络编程机制实现FTP服务器; 2. 实现从客户端能够下载服务器端的文件; 3. 实现能够从客户端上传到服务器端的文件; 4. 实现客户端能够改变服务器端的当前目录; 5. 实现查看当前服务器工作目录下的文件。
三、设计报告的内容
1. 设计题目与设计任务(设计任务书)
设计题目:利用Socket编程实现FTP服务器
设计要求:任选一门自己熟悉的程序设计语言,利用Socket网络编程机制实现FTP服务器。
2. 前言(绪论)(设计的目的、意义等)
FTP以它所使用的协议:文件传输协议(File Transfer Protocol)来命名的。正如其名所示:协议的任务是从一台计算机将文件传送到另一台计算机,它与这两台计算机所处的位置、联系的方式、以及使用的操作系统无关。假设两台计算机能与FTP协议对话,并且能访问INTERNET,就可以用FTP软件的命令来传输文件。对于不同的操作系统具体操作上可能会有些细微差别,但是其基本的命令结构是相同的。
FTP采用“客户机/服务器”方式,socket客户机与服务器之间的通信方式如图1所示。
计算机与通信工程学院课程设计 第11页 Server建立服务器侦听socket等待并接收连接请求接收请求后创建连接soket建立连接创建连接socket向服务端发送请求ClientInputStream开始通信OutputStreamOutputStreamInputStream关闭socket及相关资源关闭资源关闭socket及相关资源
图1 socket通信模型
FTP(File Transfer Protocol),是文件传输协议的简称。用于
Internet上
的控制文件的双向传输。同时,它也是一个应用程序。用户可以通过它把自己机器与世界各地所有运FTP协议的服务器相连,访问服务器上的资源和信息。 FTP协议在TCP/IP协议栈中的位置如表1:
表1. TCP/IP协议栈
HTTP FTP TELN SMTP DNS TFTP NMP TCP UDP IP X25 ISDN LAN WLAN FDDI ATM 应用层 传输层 互联网络层 网络接口层 当启动FTP从远程计算机拷贝文件时,事实上启动了两个程序:一个本地机器上的FTP客户端程序,它向FTP服务器提出拷贝文件的请求。另一个是启动在远程计算机的上的FTP服务器程序,它响应请求把你指定的文件传送到你的计算机中。FTP采用“客户端/服务器”方式,用户要在自己的本地计算机上安装FTP客户端程序。从根本上说,FTP协议就是在网络中各种不同的计算机之间按照
计算机与通信工程学院课程设计 第11页 TCP/IP协议来传输文件。FTP协议采用客户端/服务器(Client/Sever)模式,由FTP客户端程序和FTP服务器端程序组成。使用时,先启动FTP客户端程序与远程主机建立连接,然后向远程主机发出传输命令,远程主机在收到命令后就给予响应,并执行正确的命令。
3. 设计主体(各部分设计内容、分析、结论等)
(1) 下载文件
这部分功能是用来实现客户端从服务器下载文件到本地的功能。这部分用的是get函数来实现。 客户端代码如下:
public void get(String serName){
System.out.println(\"get\"+\"54512\"); System.out.println(\"请输入目录:\"); try{
Socket s = new Socket(serName,8888);
br = new BufferedReader(new InputStreamReader(System.in)); String downFile = br.readLine(); dos = new DataOutputStream(
new BufferedOutputStream(s.getOutputStream())); dos.writeUTF(downFile); dos.flush();
dis = new DataInputStream(new
BufferedInputStream(s.getInputStream())); int bufferSize = 8192;
byte[] buf = new byte[bufferSize]; int passedlen = 0; long len = 0;
String savePath = \"D:\\\\Backup\\\\我的文档\\\\Baidu\";
savePath = savePath+File.separator+dis.readUTF(); DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(savePath))); len = dis.readLong();
System.out.println(\"文件的长度为:\" + len + \" KB\"); System.out.println(\"开始接收文件!\"); while (true) { int read = 0;
if (dis != null) {
read = dis.read(buf); }
passedlen += read; if (read == -1) { break;
计算机与通信工程学院课程设计 第11页 }
System.out.println(\"文件接收了\" + (passedlen * 100 / len) + \"%\");
fileOut.write(buf, 0, read); }
System.out.println(\"接收完成,文件存为\" + savePath); fileOut.close(); }catch(IOException e ){ }
try{
dis.close(); dos.close(); s.close();
}catch(IOException e1 ){ }
} 客户端运行结果截图如图2:
图2. get 函数客户端运行结果
服务器端get函数如下: public void get(){
System.out.println(\"get\"+1111);
Socket s = null; try{
s = ss.accept();
dis = new DataInputStream(new BufferedInputStream(s .getInputStream()));
String filePath = dis.readUTF(); System.out.println(filePath);
dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
File file = new File(filePath);
计算机与通信工程学院课程设计 第11页 dos.writeUTF(file.getName()); dos.flush();
dos.writeLong(file.length()); dos.flush();
dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
int BUFSIZE = 8192;
byte [] buf = new byte[BUFSIZE];
while(true){
int read = 0; if(dis != null){
read = dis.read(buf); }else{
System.out.println(\"no file founded!\"); break; }
if (read == -1){ break; }
dos.write(buf, 0, read); }
dos.flush();
}catch(IOException e){
System.out.println(\"asdfsssssssssss\"); }finally{ try{
dos.close(); dis.close(); s.close();
}catch(IOException e){ } }
} 服务器端get函数运行结果截图如图3:
计算机与通信工程学院课程设计 第11页
图3.get 函数服务器端运行结果
(2) 上传文件
上传文件实现从本地上传文件到服务器端。这部分功能用的是put函数来实现。
客户端put函数代码如下:
public void put(String serName){ System.out.println(\"put\"); Socket s = null; try{
s = new Socket (serName,8888);
br = new BufferedReader(new InputStreamReader(System.in)); String upFile = br.readLine();
dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream())); File file = new File(upFile); dos.writeUTF(file.getName()); dos.flush();
dos.writeLong(file.length()); dos.flush();
dis = new DataInputStream(new BufferedInputStream(new FileInputStream(upFile)));
int BUFSIZE = 8192;
byte [] buf = new byte[BUFSIZE];
while(true){
int read = 0; if(dis != null){
read = dis.read(buf); }else{
System.out.println(\"no file founded!\"); break; }
if (read == -1){ break;
计算机与通信工程学院课程设计 第11页
}
dos.write(buf, 0, read); }
dos.flush(); }catch(IOException e){
}finally{
try {
dis.close(); dos.close(); s.close();
} catch (IOException e) {
e.printStackTrace(); } } }
客户端运行结果如图4:
图4.put函数客户端运行结果
服务器端代码如下:
public void put(){
System.out.println(\"put\"); Socket s = null; try{
s = ss.accept();
dis = new DataInputStream(new
BufferedInputStream(s.getInputStream()));//从客户端接收存放上传文件路径的输出流
int bufferSize = 8192;
byte[] buf = new byte[bufferSize]; int passedlen = 0;
long len = 0;
String savePath = \"D:\\\\Backup\\\\我的文档\\\\网络课程设计共享文件\";
计算机与通信工程学院课程设计 第11页 savePath = savePath+File.separator+dis.readUTF(); DataOutputStream fileOut = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(savePath)));
len = dis.readLong();
System.out.println(\"文件的长度为:\" + len + \" KB\"); System.out.println(\"开始接收文件!\");
while (true) { int read = 0;
if (dis != null) {
read = dis.read(buf); }
passedlen += read; if (read == -1) { break; }
System.out.println(\"文件接收了\" + (passedlen * 100 / len) + \"%\");
fileOut.write(buf, 0, read); }
System.out.println(\"接收完成,文件存为\" + savePath); fileOut.close(); dis.close(); s.close();
}catch(IOException e ){ } }
服务器端运行结果如图5:
图5.put 函数服务器端运行结果
(3) 查看目录下的文件
这部分用来实现查看服务器当前工作目录下的文件。这部分功能是dir函数来实现的。 客户端代码如下:
计算机与通信工程学院课程设计 第11页 public void dir ( ) throws IOException{ rootDirectory = new
File(shareFiledirectory);//shareFiledirectory表示共享文件的路径 fileArrayList.clear();
initFileArrayList();
for(int i =0;i direcFile+fileArrayList.get(i).getAbsolutePath()+'\\n'; } try{ Socket s = ss.accept(); dos = new DataOutputStream( new BufferedOutputStream(s.getOutputStream())); byte []buf = direcFile.getBytes(); dos.write(buf); dos.flush(); dos.close(); s.close(); }catch(IOException e){ } } 客户端运行结果截图如图6: 图6. dir函数客户端运行结果 服务器端代码如下: public void dir ( ) throws IOException{ rootDirectory = new File(shareFiledirectory);//shareFiledirectory表示共享文件的路径 fileArrayList.clear(); initFileArrayList(); for(int i =0;i System.out.println(fileArrayList.get(i).getAbsolutePath()); direcFile = direcFile+fileArrayList.get(i).getAbsolutePath()+'\\n'; } try{ Socket s = ss.accept(); dos = new DataOutputStream( new BufferedOutputStream(s.getOutputStream())); byte []buf = direcFile.getBytes(); dos.write(buf); dos.flush(); dos.close(); s.close(); }catch(IOException e){ } } 服务器端运行结果截图如图7: 图7.dir函数服务器端运行结果 (4) 改变工作目录 这部分功能是改变服务器当前工作目录。并查看该文件下面的文件。这部分功能用cd函数实现 客户端代码如下: public void cd(String serName){ System.out.println(\"cd\"); try{ Socket s = new Socket(serName,8888); br = new BufferedReader(new InputStreamReader(System.in)); String changedDir = br.readLine(); DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(s.getOutputStream())); dos.writeUTF(changedDir); dos.flush(); 计算机与通信工程学院课程设计 第11页 dos.close(); s.close(); }catch(IOException e){ } } 客户端运行结果截图如图8: 图8. cd 函数客户端运行结果 服务器端代码如下: public void cd(){ System.out.println(\"cd\"); Socket s = null; try{ s = ss.accept(); dis = new DataInputStream(new BufferedInputStream(s .getInputStream())); shareFiledirectory = dis.readUTF(); System.out.println(shareFiledirectory); dir(); }catch(IOException e){ }finally{ } } 服务器端运行结果如图9: 计算机与通信工程学院课程设计 第11页 图9. cd函数服务器端运行结果 4. 结束语(设计的收获、体会等) 这是一个网络编程实验,不得不说,对于网络编的一塌糊涂,首先它过多的库函数给我们带来了极大的困扰;其次,平时学的都只是空洞洞的协议,而一到真刀实战的时候往往显得准备不足。在做这个实验的时候遇到了如下的困难: (1) 刚开始的时候,对于get指令企图直接发送数据,忘记了TCP协议中发送的是socket,导致无论如何都得不出结果,后来认真研究过tcp之后,才豁然开朗,可见平时学习的不够扎实。 (2) 在server中一开始始终无法创建文件,后发现原来参数传递中,传递了错误的socket,本应适用接受后的sock,但是由于对程序理解的混淆,导致传递有误,其实这只是很小的一个错误,我列出这个错误的目的在于告诉自己应该细心,这种错误是不该犯的,而我却经常出现此类错误,白白浪费了很多时间。 其实实验中遇到的问题还有很多,这里不能一一枚举,说了一些时刻来警示自己,希望下次有所进步。 5. 参考资料 [1] 邓全良,《Winsock网络程序设计》,中国铁道出版社 [2] 王罡 林立志,《基于Windows 的TCP/IP编程》,清华大学出版社 想要项目代码可以去csdn下载: https://download.csdn.net/download/thinktimes/10583629 可以直接在eclipse中运行。 要先运行服务端程序,然后运行客户端程序。剩下的运行相信大家看源码都能看懂逻辑,就不介绍了。 计算机与通信工程学院课程设计 第11页 四、设计时间与安排 1、设计时间:2周 2、设计时间安排: 熟悉实验设备、收集资料:设计图纸、实验、计算、程序编写调试:编写课程设计报告:答辩:2 天 8 天 1 天 1 天
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务