基本信息
源码名称:FTP文件传输(入门级示例)
源码大小:0.14M
文件格式:.zip
开发语言:C#
更新时间:2018-12-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
采用FTP PASV模式设计一个FTP服务器程序和一个FTP客户机程序,具有文件夹内容浏览和文件下载功能,服务器程序能够接收多个客户机的FTP请求并且能够对客户机身份进行验证。
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;
}
}
}
}
}