欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

FTP文件传输(入门级示例)

介绍 评论 失效链接反馈

采用FTP PASV模式设计一个FTP服务器程序和一个FTP客户机程序,具有文件夹内容浏览和文件下载功能,服务器程序能够接收多个客户机的FTP请求并且能够对客户机身份进行验证。

from clipboard
from clipboard using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;using System.IO;using System.Collections;using System.Net.NetworkInformation;namespace client{ public partial class Form1 : Form { TcpClient controltc; NetworkStream controlns; StreamReader controlsr; StreamWriter controlsw; private static string ext =null; public Form1() { InitializeComponent(); this.buttonUpDir.Enabled= false; Control.CheckForIllegalCrossThreadCalls = false; } private void closecontrolconnection() { this.controltc.Close(); this.controlns.Close(); this.controlsr.Close(); this.controlsw.Close(); } private void buttonConnect_Click(object sender, EventArgs e) { try { controltc = new TcpClient("192.168.1.6", 21); } catch (Exception ee) { MessageBox.Show("与服务器连接失败!" ee.ToString()); return; } controlns = controltc.GetStream(); controlsr = new StreamReader(controlns, System.Text.Encoding.Unicode); controlsw = new StreamWriter(controlns, System.Text.Encoding.Unicode); string str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); controlsw.WriteLine("USER ftpuser"); controlsw.Flush(); str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到" str); if (str == "421") { MessageBox.Show("用户名不正确"); this.closecontrolconnection(); return; } controlsw.WriteLine("PASS ftppass"); controlsw.Flush(); str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); if (str == "421") { MessageBox.Show("密码不正确"); this.closecontrolconnection(); return; } //获取FTP根目录下的子目录和文件列表 GetDirAndFiles(@"server:\"); } private void button1_Click(object sender, EventArgs e) { } private void GetDirAndFiles(string path) { this.groupBoxDir.Text = path; //-------判断当前目录是否为虚拟根目录--------- if (path == @"server:\") { this.buttonUpDir.Enabled = false; } else { this.buttonUpDir.Enabled = true; } TcpClient datatc = this.getdataconnection(); NetworkStream datans = datatc.GetStream(); StreamReader datasr = new StreamReader(datans); //获取目录和文件列表 controlsw.WriteLine("LIST " path); controlsw.Flush(); this.listBoxInfo.Items.Add("发送:LIST " path); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; string str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; if (str == "125") //表示服务器已经准备好数据,并开始传送 { //获取子目录列表 this.listBoxDir.Items.Clear(); str = datasr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; string[] strarray; //字符串长度为0,说明当前目录下没有子目录 if (str.Length > 0) { strarray = str.Split('@'); for (int i = 0; i < strarray.Length; i ) { this.listBoxDir.Items.Add(strarray[i] "\\"); } } //获取文件列表 this.listBoxFile.Items.Clear(); str = datasr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; //字符串长度为0,说明当前目录下没有文件 if (str.Length > 0) { strarray = str.Split('@'); for (int i = 0; i < strarray.Length; i ) { this.listBoxFile.Items.Add(strarray[i]); } } datasr.Close(); datans.Close(); datatc.Close(); this.buttonDownload.Enabled = false; str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; } } private TcpClient getdataconnection() { controlsw.WriteLine("PASV "); controlsw.Flush(); string str = controlsr.ReadLine(); if (str != "227") { MessageBox.Show("FTP被动模式失败!"); } str = controlsr.ReadLine(); int rdp = int.Parse(str.Substring(str.IndexOf(" ") 1)); TcpClient datatc = new TcpClient("127.0.0.1",rdp); controlsw.WriteLine("150"); controlsw.Flush(); return datatc; } private void buttonUpDir_Click(object sender, EventArgs e) { string path = this.groupBoxDir.Text; path = path.Substring(0,path.LastIndexOf("\\")); int num = path.LastIndexOf("\\"); path = path.Substring(0, num 1); GetDirAndFiles(path); } private void buttonDisConnect_Click(object sender, EventArgs e) { controlsw.WriteLine("QUIT"); controlsw.Flush(); this.listBoxInfo.Items.Add("发送:QUIT"); string str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; controltc.Close(); } private void listBoxDir_SelectedIndexChanged(object sender, EventArgs e) { if (this.listBoxDir.SelectedIndex == -1) { GetDirAndFiles(this.groupBoxDir.Text); } else { GetDirAndFiles(this.listBoxDir.SelectedItem.ToString()); } } private void listBoxFile_SelectedIndexChanged(object sender, EventArgs e) { if (this.listBoxFile.SelectedIndex == -1) { this.buttonDownload.Enabled = false; } else { this.buttonDownload.Enabled = true; } } private string getfileext(string filename) { try { char[] point = new char[] { ',' }; string[] filename2 = filename.Split(point); return filename2[1]; } catch { return null; } } private void buttonDownload_Click(object sender, EventArgs e) { ext = getfileext(this.listBoxFile.Text); SaveFileDialog myfile = new SaveFileDialog(); myfile.Filter = ext " files" "(*." ext ")|*." ext "|All files (*.*)|*.*"; if (myfile.ShowDialog() == DialogResult.OK) { //重画窗体内的所有控件,使窗体显示完整 foreach (Control control in this.Controls) { control.Update(); } TcpClient datatc = this.getdataconnection(); NetworkStream datans = datatc.GetStream(); StreamReader datasr = new StreamReader(datans); string path = this.listBoxFile.SelectedItem.ToString(); controlsw.WriteLine("RETR " path); controlsw.Flush(); this.listBoxInfo.Items.Add("发送:RETR " path); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; string str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; if (str == "125") //表示服务器文件状态良好 { string str1 = datasr.ReadLine(); this.listBoxInfo.Items.Add("文件长度:" str1 "字节"); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1; this.lbdf.Text="正在下载``````"; FileStream fs = new FileStream(myfile.FileName, FileMode.Create, FileAccess.Write); byte[] byteF = TransData.ReceiveVarData(datatc.Client); if (byteF.Length == 0) { MessageBox.Show("??!!"); } fs.Write(byteF,0,byteF.Length); fs.Flush(); fs.Close(); datasr.Close(); datans.Close(); datatc.Close(); MessageBox.Show("下载完毕!"); this.lbdf.Text = ""; str = controlsr.ReadLine(); this.listBoxInfo.Items.Add("收到:" str); this.listBoxInfo.SelectedIndex = this.listBoxInfo.Items.Count - 1;} } } }}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复