首页 范文大全 古典文学 职场知识 中国文学 公文书信 外国名著 寓言童话 百家讲坛 散文/诗歌 美文欣赏 礼仪知识 民俗风情
  • 工作总结
  • 工作计划
  • 心得体会
  • 竞聘演讲
  • 会议发言
  • 爱国演讲
  • 就职演说
  • 开业开幕
  • 思想学习
  • 征文演讲
  • 经验材料
  • 述职报告
  • 调研报告
  • 工作汇报
  • 年终总结
  • 申报材料
  • 学习体会
  • 企划方案
  • 活动方案
  • 技巧经验
  • 模板范例
  • 思想宣传
  • 经济工作
  • 工作报告
  • 组织人事
  • 反腐倡廉
  • 慰问贺电
  • 先进事迹
  • 思想汇报
  • 入党申请书
  • 党会发言
  • 先进性教育
  • 入团申请书
  • 个人简历
  • 演讲稿
  • 调查报告
  • 实习报告
  • 和谐社会
  • 观后感
  • 读后感
  • 作文范文
  • 自我鉴定
  • 讲话稿
  • 自查报告
  • 计网socket编程实验报告

    时间:2020-11-15 12:03:00 来源:蒲公英阅读网 本文已影响 蒲公英阅读网手机站

    相关热词搜索:编程 实验 报告

     深 深 圳 大 学 实 验 报 告 告

      实验课程名称:

     计算机网络

     实验项目名称:

     Socket 编程

     学院:

     计算机与软件学院

     专业:

     计算机科学与技术

     报告人:

     学号:

     班级:

     同组人:

     指导教师:

     实验时间:

     2015- -0 05 5- - 10

     提交时间:

     5 2015 年 年 5 5 月 月 9 29 日

     声明:

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

     教务处制

      一、 实验目的

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

     二、 实验要求 用 Socket (WinSock)编程,实现简单的 FTP 客户端:

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

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

     使用 PORT(或 PASV)

     建立数据连接。

     使用 NLST 进行文件列表。

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

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

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

     lab:lab @ ftp.case.szu.edu.cn

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

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

      三、 实验 分析设计 (1)服务端启动,等待用户连接

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

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

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

     后进行解析,作出

     相应的响应

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

     四、 核心代码说明 #pragma comment(lib,"ws2_32")

     #include<WinSock.h>

     #include<fstream>

     #include<iostream>

     #include<errno.h>

     #include<time.h>

     #include<string>

     #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"<<endl;

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

      用户登录模块

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

      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"<<endl;

      else

     cout<<"Error Check input!"<<endl;

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

      cout<<userName<<endl;

      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<<MENU<<endl;

     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<<Dir<<endl;

      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)::"<<endl;

      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)

     //进入删除文件的程序代码段

     {

      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->sendCommand();

      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."<<endl;

      cout<<"Cleaning system resource"<<endl;

      //delete [] userName;

      //delete [] Direct;

      //delete client;

      cout<<"Exiting Goodbye"<<endl;

      system("pause");

     }

     五、 测试 与结果

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

      七、参考资料 ①《现代网络技术教程——自顶向下分析与设计》

     ②百度百科

     ③百度知道

    • 范文大全
    • 职场知识
    • 精美散文
    • 名著
    • 讲坛
    • 诗歌
    • 礼仪知识