搜档网
当前位置:搜档网 › 计网socket编程实验报告

计网socket编程实验报告

计网socket编程实验报告
计网socket编程实验报告

深圳大学实验报告

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

实验项目名称: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();

}

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

memset(Dir,NULL,512);

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

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->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) //进入删除文件的程序代码段

{

char* filename = new char[512];

memset(filename,NULL,strlen(filename));

cout<<"Please input the filename you want to Delete:";

cin>>filename;

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

client->receiveCommand();

delete filename;

}

else if(strcmp(Command,"rmd")==0||strcmp(Command,"RMD")==0) //进入删除目录的程序代码段

{

memset(Dir,NULL,strlen(Dir));

cout<<"Please input the Direct you want to Delete:";

cin>>Dir;

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

client->sendCommand();

client->receiveCommand();

}

else if(strcmp(Command,"mkd")==0||strcmp(Command,"MKD")==0) //进入修改文件的程序代码段

{

memset(Dir,NULL,strlen(Dir));

cout<<"Please input the Direct you want to Make:";

cin>>Dir;

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

client->sendCommand();

client->receiveCommand();

}

else if(strcmp(Command,"QUIT")==0||strcmp(Command,"quit")==0) //进入退出文件的程序代码段

{

break;

}

else

{

cout<<"No such COMMAND!!";

}

}

}

}

else

cout<<"Error You can not login in."<

cout<<"Cleaning system resource"<

//delete [] userName;

//delete [] Direct;

//delete client;

cout<<"Exiting Goodbye"<

system("pause");

}

五、测试与结果

六、总结与分析

本次实验达到了本项实验的初始目的和要求。通过这次实验,我对socket的网络编程有了更清楚的认识,懂得了它怎么联网并对ftp上的文件的上传,下载等操作,知道了怎么把书上的知识在实际中的运用。不过由于对界面的设计还不是很了解,还不能很好地把界面设计出来。希望下次能做得更好!

七、参考资料

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

②百度百科

③百度知道

相关主题