计网socket编程实验报告

  • 格式:doc
  • 大小:533.00 KB
  • 文档页数:10

下载文档原格式

  / 22
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

深圳大学实验报告

实验课程名称:计算机网络

实验项目名称:Socket 编程

学院:计算机与软件学院专业:计算机科学与技术报告人:学号:班级:

同组人:

指导教师:

实验时间:2015-05-10 提交时间:2015年5月29日

声明:

本次实验内容由报告人和同组人独立完成,所有涉及到他人的工作均已说明。报告人和同组人均同意教师及学校为教学活动而引用本实验的内容,且无需事先征得同意和特别说明。

教务处制

一、实验目的

了解FTP协议的工作原理,掌握基于socket的网络编程的基本原理。

二、实验要求

用Socket (WinSock)编程,实现简单的FTP 客户端:

客户端和FTP 服务器建立Socket 连接。

向服务器发送USER、PASS 命令登录FTP 服务器。

使用PORT(或PASV)建立数据连接。

使用NLST 进行文件列表。

使用RETR / STOR 命令下载/上传文件。

在下载完毕后断开数据连接并发送QUIT 命令退出。

服务器:Apache Friends 中的FileZillaFTP,或是

lab:lab @

在整个交互的过程中,控制连接始终处于连接的状态。

数据连接在每传输一个文件时先打开,传输后关闭

三、实验分析设计

(1)服务端启动,等待用户连接

(2)客户端启动,请求与服务端连接

(3)服务端应答,与用户建立连接

(4)用户输入目录操作、文件上传下载等指令,服务端接收到指令后进行解析,作出

相应的响应

(5)重复(4)的过程,直至用户输入quit指令要求离开,服务结束

四、核心代码说明

#pragma comment(lib,"ws2_32")

#include

#include

#include

#include

#include

#include

#include"ftpClient.h"

using namespace std;

#define MENU "Welcome To The FTP Server,Please Input The Command And Enter!Such as: LIST,CWD,RETR,STOR,DELE,RMD,MKD"

//定义了在ftp服务器上能进行的操作

int main()

{

char Dir[256];

memset(Dir,NULL,256);

int returnNum;

char ip[16];

int port;

char test;

char userName[50];

memset(userName,NULL,50);

strncpy(userName,"anonymous",strlen("anonymous"));

char PWD[50];

char temp[512];

char Command[4];

//char Parameter[256];

cout<<"Please input the ip of the FTP server::";

cin>>ip;

cout<<"Do you want to change the port,Now the port is 21 :[Y/N]";

//使用命令端口21来连接到ftp服务器,在ftp协议下不用更改

cin>>test;

if(test=='Y'||test=='y')//连接到ftp服务器

{

cout<<"Please input the num of the port::";

cin>>temp;

port =(temp[0] - '0')*100+(temp[1] - '0')*10+(temp[2] - '0'); //将字符型转化为数值型

memset(temp,NULL,2);

}

else if (test=='N'||test=='n')

port = 21;

else

cout<<"Error"<

/************************************

用户登录模块

************************************/

ftpClient* client = new ftpClient(ip,port);

cout<<"Do you want to change USERNAME,Now is anonymous:[Y/N]"; //不更改时默认的是匿名登录

cin>>test;

if(test=='Y'||test=='y')

{

cout<<"UserName::";

cin>>userName;//输入登录名

}

else if (test=='N'||test=='n')

cout<<"UserName::anonymous"<

else

cout<<"Error Check input!"<

client->setCommand("USER",userName);//验证登录名

cout<

client->sendCommand(); //向ftp服务器发送用户名

returnNum = client->receiveCommand(); //从ftp服务器接收到的响应码,正确时应为331

if(returnNum == 331)

{

cout<<"PassWord::";

cin>>PWD;//输入密码

client->setCommand("PASS",PWD);

client->sendCommand();

returnNum = client->receiveCommand(); //从ftp服务器接收响应码,正确时应返回230

if(returnNum == 230) //用户已经正确登录到了ftp服务器

{

while(1)

{

cout<

cout<<"FTP::>";

cin>>Command;

if(strcmp(Command,"list")==0||strcmp(Command,"LIST")==0)

{

/************************************

连接控制,传输控制,命令传输(需使用

socketData连接的命令,如:LIST)

************************************/

client->setCommand("PASV");

client->sendCommand();

client->receiveCommand();

client->getPort();

client->setCommand("TYPE","I");

client->sendCommand();

client->receiveCommand();

client->interlizeDataSocket();

client->setCommand("LIST", ".");

cout<

client->sendCommand();

client->receiveCommand();

client->receiveList();

client->receiveCommand();

}