1、计网socket编程实验报告深 圳 大 学 实 验 报 告实验课程名称: 计算机网络 实验项目名称: Socket 编程 学院: 计算机与软件学院 专业: 计算机科学与技术 报告人: 学号: 班级: 同组人: 指导教师: 实验时间: 2015-05-10 提交时间: 2015年5月29日 声明: 本次实验内容由报告人和同组人独立完成,所有涉及到他人的工作均已说明。报告人和同组人均同意教师及学校为教学活动而引用本实验的内容,且无需事先征得同意和特别说明。教务处制1、实验目的 了解FTP协议的工作原理,掌握基于socket的网络编程的基本原理。2、实验要求用 Socket (WinSock)编程,
2、实现简单的 FTP 客户端:客户端和 FTP 服务器建立 Socket 连接。 向服务器发送 USER、PASS 命令登录 FTP 服务器。 使用 PORT(或PASV) 建立数据连接。 使用 NLST 进行文件列表。 使用 RETR / STOR 命令下载/上传文件。 在下载完毕后断开数据连接并发送 QUIT 命令退出。服务器:Apache Friends 中的 FileZillaFTP,或是 lab:lab 在整个交互的过程中,控制连接始终处于连接的状态。数据连接在每传输一个文件时先打开,传输后关闭3、实验分析设计(1)服务端启动,等待用户连接(2)客户端启动,请求与服务端连接(3)服务端
3、应答,与用户建立连接(4)用户输入目录操作、文件上传下载等指令,服务端接收到指令后进行解析,作出相应的响应(5)重复(4)的过程,直至用户输入quit指令要求离开,服务结束4、核心代码说明#pragma comment(lib,ws2_32)#include#include#include#include#include#include#includeftpClient.husing namespace std;#define MENU Welcome To The FTP Server,Please Input The Command And Enter!Such as: LIST,CWD,
4、RETR,STOR,DELE,RMD,MKD/定义了在ftp服务器上能进行的操作int main() char Dir256; memset(Dir,NULL,256); int returnNum; char ip16; int port; char test; char userName50; memset(userName,NULL,50); strncpy(userName,anonymous,strlen(anonymous); char PWD50; char temp512; char Command4; /char Parameter256; coutip; couttest;
5、 if(test=Y|test=y)/连接到ftp服务器 couttemp; port =(temp0 - 0)*100+(temp1 - 0)*10+(temp2 - 0); /将字符型转化为数值型 memset(temp,NULL,2); else if (test=N|test=n) port = 21; else coutErrorendl; /* 用户登录模块 */ ftpClient* client = new ftpClient(ip,port); couttest; if(test=Y|test=y) coutuserName;/输入登录名 else if (test=N|te
6、st=n) coutUserName:anonymousendl; else coutError Check input!setCommand(USER,userName);/验证登录名 coutuserNamesendCommand(); /向ftp服务器发送用户名 returnNum = client-receiveCommand(); /从ftp服务器接收到的响应码,正确时应为331 if(returnNum = 331) coutPWD;/输入密码 client-setCommand(PASS,PWD); client-sendCommand(); returnNum = client
7、-receiveCommand(); /从ftp服务器接收响应码,正确时应返回230 if(returnNum = 230) /用户已经正确登录到了ftp服务器 while(1) coutMENUendl; cout; cinCommand; if(strcmp(Command,list)=0|strcmp(Command,LIST)=0) /* 连接控制,传输控制,命令传输(需使用 socketData连接的命令,如:LIST) */ client-setCommand(PASV); client-sendCommand(); client-receiveCommand(); client-
8、getPort(); client-setCommand(TYPE,I); client-sendCommand(); client-receiveCommand(); client-interlizeDataSocket(); client-setCommand(LIST, .); coutDirsendCommand(); client-receiveCommand(); client-receiveList(); client-receiveCommand(); else if(strcmp(Command,CWD)=0|strcmp(Command,cwd)=0) memset(Dir
9、,NULL,512); coutPlase input the dir of your will(compelete dir):Dir; client-setCommand(CWD ,Dir); client-sendCommand(); client-receiveCommand(); else if(strcmp(Command,retr)=0|strcmp(Command,RETR)=0) char* filename = new char512; memset(filename,NULL,512); /* 下载模块 */ client-setCommand(PASV); client-
10、sendCommand(); client-receiveCommand(); client-getPort(); client-setCommand(TYPE,I); client-sendCommand(); client-receiveCommand(); client-interlizeDataSocket(); coutfilename; client-setCommand(RETR ,filename); client-sendCommand(); client-receiveCommand(); client-receiveData(filename); client-recei
11、veCommand(); delete filename; else if(strcmp(Command,stor)=0|strcmp(Command,STOR)=0) char* filename = new char512; memset(filename,NULL,512); /* 上传模块 */ client-setCommand(PASV); client-sendCommand(); client-receiveCommand(); client-getPort(); client-setCommand(TYPE,I); client-sendCommand(); client-r
12、eceiveCommand(); client-interlizeDataSocket(); coutfilename; client-setCommand(STOR ,filename); client-sendCommand(); client-receiveCommand(); client-sendData(filename); client-receiveCommand(); delete filename; else if(strcmp(Command,dele)=0|strcmp(Command,DELE)=0)/进入删除文件的程序代码段 char* filename = new
13、 char512; memset(filename,NULL,strlen(filename); coutfilename; client-setCommand(DELE ,filename); client-sendCommand(); client-receiveCommand(); delete filename; else if(strcmp(Command,rmd)=0|strcmp(Command,RMD)=0)/进入删除目录的程序代码段 memset(Dir,NULL,strlen(Dir); coutDir; client-setCommand(RMD ,Dir); clien
14、t-sendCommand(); client-receiveCommand(); else if(strcmp(Command,mkd)=0|strcmp(Command,MKD)=0)/进入修改文件的程序代码段 memset(Dir,NULL,strlen(Dir); coutDir; client-setCommand(MKD ,Dir); client-sendCommand(); client-receiveCommand(); else if(strcmp(Command,QUIT)=0|strcmp(Command,quit)=0)/进入退出文件的程序代码段 break; els
15、e coutNo such COMMAND!; else coutError You can not login in.endl; coutCleaning system resourceendl; /delete userName; /delete Direct; /delete client; coutExiting Goodbyeendl; system(pause);5、测试与结果6、总结与分析本次实验达到了本项实验的初始目的和要求。通过这次实验,我对socket的网络编程有了更清楚的认识,懂得了它怎么联网并对ftp上的文件的上传,下载等操作,知道了怎么把书上的知识在实际中的运用。不过由于对界面的设计还不是很了解,还不能很好地把界面设计出来。希望下次能做得更好! 七、参考资料现代网络技术教程自顶向下分析与设计XX百科XX知道
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1