收藏 分享(赏)

网络编程-实验报告.doc

上传人:cjc2202537 文档编号:5231279 上传时间:2019-02-13 格式:DOC 页数:78 大小:380KB
下载 相关 举报
网络编程-实验报告.doc_第1页
第1页 / 共78页
网络编程-实验报告.doc_第2页
第2页 / 共78页
网络编程-实验报告.doc_第3页
第3页 / 共78页
网络编程-实验报告.doc_第4页
第4页 / 共78页
网络编程-实验报告.doc_第5页
第5页 / 共78页
点击查看更多>>
资源描述

1、贵州大学实验报告学院:计算机学院 专业:软件工程 班级:软件 123 班 姓名 * 学号 * 实验组实验时间 2015-5-9 指导教师 蔡丽 成绩实验项目名称 FTP 上传下载器编程实验目的通过本实验掌握 C#中 FTP 上传下载器编程的方法,了解其区别与适用场合。实验要求了解 C#的 UDP 编程方法。实验原理使用.NET 请求/响应模型的 FtpWebRequest 类和 FtpWebResponse 类实现简单的 Web 浏览器实验环境Visual Studio 开发环境实验步骤1. 设计程序界面。2. 实现程序功能。实验内容实现简单的 Web 浏览器,要求使用.NET 请求/响应模型

2、的FtpWebRequest 类和 FtpWebResponse 类。实验数据服务器核心代码/FtpServerForm 类:using System;using System.Collections.Generic;using System.Globalization;using System.IO;using System.Net;using System.Net.Sockets;using System.Threading;using System.Windows.Forms;namespace FtpServerpublic partial class FtpServerForm :

3、FormTcpListener myTcpListener = null;private Thread listenThread;/ 保存用户名和密码Dictionary users;public FtpServerForm()InitializeComponent();/ 初始化用户名和密码users = new Dictionary();users.Add(“admin“, “admin“);/ 设置默认的主目录tbxFtpRoot.Text = “F:/MyFtpServerRoot/“;IPAddress ips = Dns.GetHostAddresses(“);tbxFtpServ

4、erIp.Text = ips1.ToString();tbxFtpServerPort.Text = “21“;lstboxStatus.Enabled = false;/ 启动服务器private void btnFtpServerStartStop_Click(object sender, EventArgs e)if (myTcpListener = null)listenThread = new Thread(ListenClientConnect);listenThread.IsBackground = true;listenThread.Start();lstboxStatus.

5、Enabled = true;lstboxStatus.Items.Clear();lstboxStatus.Items.Add(“已经启动Ftp服务.“);btnFtpServerStartStop.Text = “停止“;elsemyTcpListener.Stop();myTcpListener = null;listenThread.Abort();lstboxStatus.Items.Add(“Ftp服务已停止!“);lstboxStatus.TopIndex = lstboxStatus.Items.Count - 1;btnFtpServerStartStop.Text = “启

6、动“;/ 监听端口,处理客户端连接private void ListenClientConnect()myTcpListener = new TcpListener(IPAddress.Parse(tbxFtpServerIp.Text), int.Parse(tbxFtpServerPort.Text);/ 开始监听传入的请求myTcpListener.Start();AddInfo(“启动FTP服务成功!“);AddInfo(“Ftp服务器运行中.点击”停止“按钮停止FTP服务“);while (true)try/ 接收连接请求TcpClient tcpClient = myTcpList

7、ener.AcceptTcpClient();AddInfo(string.Format(“客户端(0)与本机(1)建立Ftp连接“, tcpClient.Client.RemoteEndPoint, myTcpListener.LocalEndpoint);User user = new User();mandSession = new UserSeesion(tcpClient);user.workDir = tbxFtpRoot.Text;Thread t = new Thread(UserProcessing);t.IsBackground = true;t.Start(user);c

8、atchbreak;/ 处理客户端用户请求private void UserProcessing(object obj)User user = (User)obj;string sendString = “220 FTP Server v1.0“;RepleyCommandToUser(user, sendString);while (true)string receiveString = null;try/ 读取客户端发来的请求信息receiveString = mandSession.streamReader.ReadLine();catch(Exception ex)if (mandSe

9、ssion.tcpClient.Connected = false)AddInfo(string.Format(“客户端(0断开连接!)“, mandSession.tcpClient.Client.RemoteEndPoint);elseAddInfo(“接收命令失败!“ + ex.Message);break;if (receiveString = null)AddInfo(“接收字符串为null,结束线程!“);break;AddInfo(string.Format(“来自0:1“, mandSession.tcpClient.Client.RemoteEndPoint, receive

10、String);/ 分解客户端发来的控制信息中的命令和参数string command = receiveString;string param = string.Empty;int index = receiveString.IndexOf( );if (index != -1)command = receiveString.Substring(0, index).ToUpper();param = receiveString.Substring(command.Length).Trim();/ 处理不需登录即可响应的命令(这里只处理QUIT)if (command = “QUIT“)/ 关

11、闭TCP连接并释放与其关联的所有资源mandSession.Close();return;elseswitch (user.loginOK)/ 等待用户输入用户名:case 0:CommandUser(user, command, param);break;/ 等待用户输入密码case 1:CommandPassword(user, command, param);break;/ 用户名和密码验证正确后登陆case 2:switch (command)case “CWD“:CommandCWD(user, param);break;case “PWD“:CommandPWD(user);bre

12、ak;case “PASV“:CommandPASV(user);break;case “PORT“:CommandPORT(user, param);break;case “LIST“:CommandLIST(user, param);break;case “NLIST“:CommandLIST(user, param);break;/ 处理下载文件命令case “RETR“:CommandRETR(user, param);break;/ 处理上传文件命令case “STOR“:CommandSTOR(user, param);break;/ 处理删除命令case “DELE“:Comma

13、ndDELE(user, param);break;/ 使用Type命令在ASCII和二进制模式进行变换case “TYPE“:CommandTYPE(user, param);break;default:sendString = “502 command is not implemented.“;RepleyCommandToUser(user, sendString);break;break; / 想客户端返回响应码private void RepleyCommandToUser(User user, string str)mandSession.streamWriter.WriteLin

14、e(str);AddInfo(string.Format(“向客户端(0)发送1“, mandSession.tcpClient.Client.RemoteEndPoint, str);catchAddInfo(string.Format(“向客户端(0)发送信息失败“, mandSession.tcpClient.Client.RemoteEndPoint);/ 向屏幕输出显示状态信息(这里使用了委托机制)private delegate void AddInfoDelegate(string str);private void AddInfo(string str)/ 如果调用AddInf

15、o()方法的线程与创建ListView控件的线程不在一个线程时/ 此时利用委托在创建ListView的线程上调用if (lstboxStatus.InvokeRequired = true)AddInfoDelegate d = new AddInfoDelegate(AddInfo);this.Invoke(d, str);elselstboxStatus.Items.Add(str);lstboxStatus.TopIndex = lstboxStatus.Items.Count - 1;lstboxStatus.ClearSelected();#region 处理各个命令#region

16、登录过程,即用户身份验证过程/ 处理USER命令,接收用户名但不进行验证private void CommandUser(User user, string command, string param)string sendString = string.Empty;if (command = “USER“)sendString = “331 USER command OK, password required.“;user.userName = param;/ 设置loginOk=1为了确保后面紧接的要求输入密码/ 1表示已接收到用户名,等到接收密码user.loginOK = 1; els

17、esendString = “501 USER command syntax error.“;RepleyCommandToUser(user, sendString);/ 处理PASS命令,验证用户名和密码private void CommandPassword(User user, string command, string param)string sendString = string.Empty;if (command = “PASS“)string password = null;if (users.TryGetValue(user.userName, out password)

18、if (password = param)sendString = “230 User logged in success“;/ 2表示登录成功user.loginOK = 2;elsesendString = “530 Password incorrect.“;elsesendString = “530 User name or password incorrect.“;elsesendString = “501 PASS command Syntax error.“;RepleyCommandToUser(user, sendString);/ 用户当前工作目录user.currentDi

19、r = user.workDir;#endregion#region 文件管理命令/ 处理CWD命令,改变工作目录private void CommandCWD(User user, string temp)string sendString = string.Empty;trystring dir = user.workDir.TrimEnd(/) + temp;/ 是否为当前目录的子目录,且不包含父目录名称if (Directory.Exists(dir)user.currentDir = dir;sendString = “250 Directory changed to “ + dir

20、 + “ successfully“;elsesendString = “550 Directory “ + dir + “ does not exist“;catchsendString = “502 Directory changed unsuccessfully“;RepleyCommandToUser(user,sendString);/ 处理PWD命令,显示工作目录private void CommandPWD(User user)string sendString = string.Empty;sendString = “257 “ + user.currentDir + “ is

21、 the current directory“;RepleyCommandToUser(user, sendString);/ 处理LIST/NLIST命令,想客户端发送当前或指定目录下的所有文件名和子目录名private void CommandLIST(User user, string parameter)string sendString = string.Empty;DateTimeFormatInfo dateTimeFormat = new CultureInfo(“en-US“, true).DateTimeFormat;/ 得到目录列表string dir = Directo

22、ry.GetDirectories(user.currentDir);if (string.IsNullOrEmpty(parameter) = false)if (Directory.Exists(user.currentDir + parameter)dir = Directory.GetDirectories(user.currentDir + parameter);elsestring s = user.currentDir.TrimEnd(/);user.currentDir = s.Substring(0, s.LastIndexOf(“/“) + 1);for (int i =

23、0; i 1024的随机端口/ 下面这个运算算法只是为了得到一个大于1024的端口值port = random1 8 | random2;tryuser.dataListener = new TcpListener(localip, port);AddInfo(“TCP 数据连接已打开(被动模式)-“ + localip.ToString() + “:“ + port);catchcontinue;user.isPassive = true;string temp = localip.ToString().Replace(., ,);/ 必须把端口号IP地址告诉客户端,客户端接收到响应命令后,/ 再通过新的端口连接服务器的端口P,然后进行文件数据传输sendString = “227 Entering Passive Mode(“ + temp + “,“ + random1 + “,“ + random2 + “)“;RepleyCommandToUser(user, sendString);user.dataListener.Start();break;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 实用文档 > 工作总结

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报