2024年4月26日发(作者:)

深 圳 大 学 实 验 报 告

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

实验项目名称: 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""

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"

;

cout<

dir)::"<

client->sendCommand();

client->receiveCommand();

client->receiveList();

client->receiveCommand();

}

else if(strcmp(Command,"CWD")==0||strcmp(Command,"cwd")==0)

{

memset(Dir,NULL,512);

cout<<"Plase input the dir of your will(compelete

cin>>Dir;

client->setCommand("CWD ",Dir);

client->sendCommand();

client->receiveCommand();

}

else if(strcmp(Command,"retr")==0||strcmp(Command,"RETR")==0)

{

char* filename = new char[512];

memset(filename,NULL,512);

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

下载模块

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

client->setCommand("PASV");

client->sendCommand();

client->receiveCommand();

client->getPort();

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

client->sendCommand();

client->receiveCommand();

client->interlizeDataSocket();

cout<<"Please input the name you want to download:";

cin>>filename;

client->setCommand("RETR ",filename);

client->sendCommand();

client->receiveCommand();

client->receiveData(filename);

client->receiveCommand();

delete filename;

}

else if(strcmp(Command,"stor")==0||strcmp(Command,"STOR")==0)

{

char* filename = new char[512];

memset(filename,NULL,512);

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

上传模块

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

client->setCommand("PASV");

client->sendCommand();

client->receiveCommand();

client->getPort();

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

client->sendCommand();

client->receiveCommand();

client->interlizeDataSocket();

cout<<"Please input the name you want to UPload:";

cin>>filename;

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)

<

cout<<"Cleaning system resource"<

//delete [] userName;

//delete [] Direct;

//delete client;

cout<<"Exiting Goodbye"<

system("pause");

}

五、测试与结果

六、总结与分析

本次实验达到了本项实验的初始目的和要求。通过这次实验,我对socket的网络编程

有了更清楚的认识,懂得了它怎么联网并对ftp上的文件的上传,下载等操作,知道了怎么

把书上的知识在实际中的运用。不过由于对界面的设计还不是很了解,还不能很好地把界面

设计出来。希望下次能做得更好!

七、参考资料

①《现代网络技术教程——自顶向下分析与设计》

②百度百科

③百度知道