




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机与通信工程学院课程设计 第11页 课程设计任务书专业: 学号: 学生姓名(签名): 设计题目:利用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所示。图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协议就是在网络中各种不同的计算机之间按照TCP/IP协议来传输文件。FTP协议采用客户端/服务器(Client/Sever)模式,由FTP客户端程序和FTP服务器端程序组成。使用时,先启动FTP客户端程序与远程主机建立连接,然后向远程主机发出传输命令,远程主机在收到命令后就给予响应,并执行正确的命令。3. 设计主体(各部分设计内容、分析、结论等)(1) 下载文件这部分功能是用来实现客户端从服务器下载文件到本地的功能。这部分用的是get函数来实现。客户端代码如下: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.writeUTF(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 = 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(); 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 = 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.flush(); 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 = -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)System.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(); 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) 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 BufferedInputStream(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 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函数来实现的。客户端代码如下:public void dir ( ) throws IOExceptionrootDirectory = new File(shareFiledirectory);/shareFiledirectory表示共享文件的路径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 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 IOExceptionrootDirectory = new File(shareFiledirectory);/shareFiledirectory表示共享文件的路径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 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);trySocket 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();dos.close();s.close();catch(IOException e)客户端运行结果截图如图8:图8. cd 函数客户端运行结果服务器端代码如下:public void cd()System.out.println(cd);Socket s = null;trys = ss.accept();dis = new DataInputStream(new BufferedInputStream(s.getInputStream();shareFiledirectory = dis.readUTF(); System.out.println(shareFiledirectory)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年新能源行业企业数字化转型与产业升级协同路径报告
- 2025年新能源行业人才国际化趋势分析报告
- 2025年二手奢侈品鉴定与交易规范政策解读报告
- 青海省安全工程师安全生产法:高处作业的一般施工安全规定和技术措施考试试卷
- 2025年加格达奇区城市建设综合服务中心公益性岗位招聘(2人)考前自测高频考点模拟试题及答案详解(名校卷)
- 招商银行唐山市路北区2025秋招无领导小组面试案例库
- 招商银行丹东市元宝区2025秋招英文面试题库及高分回答
- 2025年高校教师资格证之《高等教育法规》模考模拟试题带答案详解(达标题)
- 招商银行江门市蓬江区2025秋招信息科技岗笔试题及答案
- 兴业银行西宁市城西区2025秋招半结构化面试题库及参考答案
- 《分子生物学基础知识》课件
- GB/T 45147-2024道路车辆总质量大于3.5 t的车辆气制动系统试验使用滚筒制动试验台获取和使用参考值
- 食管纵隔瘘护理
- 建筑项目水泥采购合同
- 华为ICT大赛网络赛道考试题库(786题)
- 水果采购协议样本
- 中职英语(高教版2021基础模块1)Part01-Unit2-Transportation
- 哲学与人生 第二课 树立科学的世界观2.1
- 2024-2030年中国止痛药品市场供需形势及未来前景动态研究研究报告
- 风电110KV升压站土建工程施工方案
- 2018低压电力线高速载波通信互联互通技术规范第3部分:检验方法
评论
0/150
提交评论