计网课设报告ftp服务器端和客户端(共13页).doc

上传人:飞****2 文档编号:14458966 上传时间:2022-05-04 格式:DOC 页数:13 大小:183KB
返回 下载 相关 举报
计网课设报告ftp服务器端和客户端(共13页).doc_第1页
第1页 / 共13页
计网课设报告ftp服务器端和客户端(共13页).doc_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《计网课设报告ftp服务器端和客户端(共13页).doc》由会员分享,可在线阅读,更多相关《计网课设报告ftp服务器端和客户端(共13页).doc(13页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、精选优质文档-倾情为你奉上课程设计任务书专业: 学号: 学生姓名(签名): 设计题目:利用Socket网络编程机制实现FTP服务器一、设计实验条件1208实验室二、设计任务及要求1. 利用Socket网络编程机制实现FTP服务器;2. 实现从客户端能够下载服务器端的文件;3. 实现能够从客户端上传到服务器端的文件;4. 实现客户端能够改变服务器端的当前目录;5. 实现查看当前服务器工作目录下的文件。三、设计报告的内容1. 设计题目与设计任务(设计任务书)设计题目:利用Socket编程实现FTP服务器设计要求:任选一门自己熟悉的程序设计语言,利用Socket网络编程机制实现FTP服务器。2. 前

2、言(绪论)(设计的目的、意义等)FTP以它所使用的协议:文件传输协议(File Transfer Protocol)来命名的。正如其名所示:协议的任务是从一台计算机将文件传送到另一台计算机,它与这两台计算机所处的位置、联系的方式、以及使用的操作系统无关。假设两台计算机能与FTP协议对话,并且能访问INTERNET,就可以用FTP软件的命令来传输文件。对于不同的操作系统具体操作上可能会有些细微差别,但是其基本的命令结构是相同的。FTP采用“客户机/服务器”方式,socket客户机与服务器之间的通信方式如图1所示。图1 socket通信模型 FTP(File Transfer Protocol),

3、是文件传输协议的简称。用于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服务器程序,它响

4、应请求把你指定的文件传送到你的计算机中。FTP采用“客户端/服务器”方式,用户要在自己的本地计算机上安装FTP客户端程序。从根本上说,FTP协议就是在网络中各种不同的计算机之间按照TCP/IP协议来传输文件。FTP协议采用客户端/服务器(Client/Sever)模式,由FTP客户端程序和FTP服务器端程序组成。使用时,先启动FTP客户端程序与远程主机建立连接,然后向远程主机发出传输命令,远程主机在收到命令后就给予响应,并执行正确的命令。3. 设计主体(各部分设计内容、分析、结论等)(1) 下载文件这部分功能是用来实现客户端从服务器下载文件到本地的功能。这部分用的是get函数来实现。客户端代码

5、如下:public void get(String serName)System.out.println(get+54512);System.out.println(请输入目录:);trySocket s = new Socket(serName,8888);br = new BufferedReader(new InputStreamReader(System.in); String downFile = br.readLine(); dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream();dos.wri

6、teUTF(downFile);dos.flush(); dis = new DataInputStream(new BufferedInputStream(s.getInputStream(); int bufferSize = 8192; byte buf = new bytebufferSize; int passedlen = 0; long len = 0; String savePath = D:Backup我的文档Baidu; savePath = savePath+File.separator+dis.readUTF(); DataOutputStream fileOut =

7、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(文件接

8、收了 + (passedlen * 100 / len) + %); fileOut.write(buf, 0, read); System.out.println(接收完成,文件存为 + savePath); fileOut.close(); catch(IOException e )trydis.close();dos.close();s.close();catch(IOException e1 )客户端运行结果截图如图2:图2. get 函数客户端运行结果服务器端get函数如下:public void get()System.out.println(get+1111);Socket s

9、= null;trys = 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); dos.writeUTF(file.getName(); dos.flu

10、sh(); dos.writeLong(file.length(); dos.flush(); dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath); int BUFSIZE = 8192; byte buf = new byteBUFSIZE; while(true) int read = 0; if(dis != null) read = dis.read(buf); else System.out.println(no file founded!); break; if (read

11、= -1) break; dos.write(buf, 0, read); dos.flush(); catch(IOException e)System.out.println(asdfsssssssssss);finallytrydos.close();dis.close();s.close();catch(IOException e)服务器端get函数运行结果截图如图3:图3.get 函数服务器端运行结果(2) 上传文件上传文件实现从本地上传文件到服务器端。这部分功能用的是put函数来实现。客户端put函数代码如下:public void put(String serName)Syste

12、m.out.println(put);Socket s = null;trys = 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()

13、; dos.writeLong(file.length(); dos.flush(); dis = new DataInputStream(new BufferedInputStream(new FileInputStream(upFile); int BUFSIZE = 8192; byte buf = new byteBUFSIZE; while(true) int read = 0; if(dis != null) read = dis.read(buf); else System.out.println(no file founded!); break; if (read = -1)

14、break; dos.write(buf, 0, read); dos.flush();catch(IOException e)finallytry dis.close();dos.close(); s.close(); catch (IOException e) e.printStackTrace();客户端运行结果如图4:图4.put函数客户端运行结果服务器端代码如下:public void put()System.out.println(put);Socket s = null;trys = ss.accept(); dis = new DataInputStream(new Buffe

15、redInputStream(s.getInputStream();/从客户端接收存放上传文件路径的输出流 int bufferSize = 8192; byte buf = new bytebufferSize; int passedlen = 0; long len = 0; String savePath = D:Backup我的文档网络课程设计共享文件; savePath = savePath+File.separator+dis.readUTF(); DataOutputStream fileOut = new DataOutputStream( new BufferedOutput

16、Stream( 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) + %); fileO

17、ut.write(buf, 0, read); System.out.println(接收完成,文件存为 + savePath); fileOut.close(); dis.close();s.close();catch(IOException e )服务器端运行结果如图5:图5.put 函数服务器端运行结果(3) 查看目录下的文件这部分用来实现查看服务器当前工作目录下的文件。这部分功能是dir函数来实现的。客户端代码如下:public void dir ( ) throws IOExceptionrootDirectory = new File(shareFiledirectory);/sh

18、areFiledirectory表示共享文件的路径fileArrayList.clear();initFileArrayList();for(int i =0;ifileArrayList.size();i+)System.out.println(fileArrayList.get(i).getAbsolutePath();direcFile = direcFile+fileArrayList.get(i).getAbsolutePath()+n;trySocket s = ss.accept();dos = new DataOutputStream(new BufferedOutputStr

19、eam(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 IOExceptionrootDirectory = new File(shareFiledirectory);/shareFiledirectory表示共享文件的路径fileArrayList.clear();init

20、FileArrayList();for(int i =0;ifileArrayList.size();i+)System.out.println(fileArrayList.get(i).getAbsolutePath();direcFile = direcFile+fileArrayList.get(i).getAbsolutePath()+n;trySocket s = ss.accept();dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream();byte buf = direcFile.getByt

21、es();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);trySocket s = new Socket(serName,8888); br = new BufferedReader(new InputStreamRead

22、er(System.in);String changedDir = br.readLine();DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream();dos.writeUTF(changedDir);dos.flush();dos.close();s.close();catch(IOException e)客户端运行结果截图如图8:图8. cd 函数客户端运行结果服务器端代码如下:public void cd()System.out.println(cd);Socket

23、s = null;trys = ss.accept();dis = new DataInputStream(new BufferedInputStream(s.getInputStream();shareFiledirectory = dis.readUTF(); System.out.println(shareFiledirectory); dir();catch(IOException e)finally服务器端运行结果如图9:图9. cd函数服务器端运行结果4. 结束语(设计的收获、体会等)这是一个网络编程实验,不得不说,对于网络编程真的一塌糊涂,首先它过多的库函数给我们带来了极大的困扰

24、;其次,平时学的都只是空洞洞的协议,而一到真刀实战的时候往往显得准备不足。在做这个实验的时候遇到了如下的困难:(1) 刚开始的时候,对于get指令企图直接发送数据,忘记了TCP协议中发送的是socket,导致无论如何都得不出结果,后来认真研究过tcp之后,才豁然开朗,可见平时学习的不够扎实。(2) 在server中一开始始终无法创建文件,后发现原来参数传递中,传递了错误的socket,本应适用接受后的sock,但是由于对程序理解的混淆,导致传递有误,其实这只是很小的一个错误,我列出这个错误的目的在于告诉自己应该细心,这种错误是不该犯的,而我却经常出现此类错误,白白浪费了很多时间。其实实验中遇到的问题还有很多,这里不能一一枚举,说了一些时刻来警示自己,希望下次有所进步。5. 参考资料1 邓全良,Winsock网络程序设计,中国铁道出版社2 王罡 林立志,基于Windows 的TCP/IP编程,清华大学出版社想要项目代码可以去csdn下载:可以直接在eclipse中运行。要先运行服务端程序,然后运行客户端程序。剩下的运行相信大家看源码都能看懂逻辑,就不介绍了。四、设计时间与安排1、设计时间:2周2、设计时间安排: 熟悉实验设备、收集资料: 2 天设计图纸、实验、计算、程序编写调试: 8 天编写课程设计报告: 1 天答辩: 1 天专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com