wince下面访问FTP的客户端.doc

上传人:啊飒飒 文档编号:10586569 上传时间:2021-05-24 格式:DOC 页数:25 大小:180.50KB
返回 下载 相关 举报
wince下面访问FTP的客户端.doc_第1页
第1页 / 共25页
wince下面访问FTP的客户端.doc_第2页
第2页 / 共25页
wince下面访问FTP的客户端.doc_第3页
第3页 / 共25页
wince下面访问FTP的客户端.doc_第4页
第4页 / 共25页
wince下面访问FTP的客户端.doc_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《wince下面访问FTP的客户端.doc》由会员分享,可在线阅读,更多相关《wince下面访问FTP的客户端.doc(25页珍藏版)》请在三一文库上搜索。

1、using System;using System.Collections.Generic;using System.Text;using System.Net.Sockets;using System.IO;using System.Net;using System.Globalization;using System.Text.RegularExpressions;namespace HX.Mobile.Ftp / / FTP 的摘要说明。 / public class FTPClient private string strRemoteHost; private int strRemot

2、ePort; private string strRemotePath; private string strRemoteUser; private string strRemotePass; private Boolean bConnected; public delegate void FTPProgressEventHandler(object sender, FTPProgressEventArgs e); #region 内部变量 / / 服务器返回的应答信息(包含应答码) / private string strMsg; / / 服务器返回的应答信息(包含应答码) / privat

3、e string strReply; / / 服务器返回的应答码 / private int iReplyCode; / / 进行控制连接的socket / private Socket socketControl; / / 传输模式 / private TransferType trType; / / 传输模式:二进制类型、ASCII类型 / public enum TransferType / / Binary / Binary, / / ASCII / ASCII ; / / 接收和发送数据的缓冲区 / private static int BLOCK_SIZE = 2048; Byte

4、 buffer = new ByteBLOCK_SIZE; / / 编码方式 / Encoding ASCII = Encoding.Default; public event FTPProgressEventHandler DownloadProgress; public event FTPProgressEventHandler UploadProgress; bool _downloading; bool _uploading; bool _cancel; #endregion #region 内部函数 #region 构造函数 / / 缺省构造函数 / public FTPClient

5、() strRemoteHost = ; strRemotePath = ; strRemoteUser = ; strRemotePass = ; strRemotePort = 21; bConnected = false; / / 构造函数 / / / / / / public FTPClient(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort) IPAddress ips; ips = Dns.GetHostEntry(remoteHost).Addre

6、ssList; strRemoteHost = ips0.ToString(); strRemotePath = remotePath; strRemoteUser = remoteUser; strRemotePass = remotePass; strRemotePort = remotePort; #endregion #region 登陆 / / FTP服务器IP地址 / public string RemoteHost get return strRemoteHost; set strRemoteHost = value; / / FTP服务器端口 / public int Remo

7、tePort get return strRemotePort; set strRemotePort = value; / / 当前服务器目录 / public string RemotePath get return strRemotePath; set strRemotePath = value; / / 登录用户账号 / public string RemoteUser set strRemoteUser = value; / / 用户登录密码 / public string RemotePass set strRemotePass = value; / / 是否登录 / public

8、bool Connected get return bConnected; #endregion #region 链接 / / 建立连接 / public void Login() socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort); / 链接 try socketControl.Connect(ep); catc

9、h (Exception ex) Console.WriteLine(ex.Message); throw new IOException(Couldnt connect to remote server); / 获取应答码 ReadReply(); if (iReplyCode != 220) Logout(); throw new IOException(strReply.Substring(4); / 登陆 SendCommand(USER + strRemoteUser); if (!(iReplyCode = 331 | iReplyCode = 230) CloseSocketCo

10、nnect();/关闭连接 throw new IOException(strReply.Substring(4); if (iReplyCode != 230) SendCommand(PASS + strRemotePass); if (!(iReplyCode = 230 | iReplyCode = 202) CloseSocketConnect();/关闭连接 throw new IOException(strReply.Substring(4); bConnected = true; / 切换到目录 ChDir(strRemotePath); / / 关闭连接 / public v

11、oid Logout() if (socketControl != null) SendCommand(QUIT); CloseSocketConnect(); #endregion #region 传输模式 / / 设置传输模式 / / 传输模式 public void SetTransferType(TransferType ttType) if (ttType = TransferType.Binary) SendCommand(TYPE I);/binary类型传输 else SendCommand(TYPE A);/ASCII类型传输 if (iReplyCode != 200) t

12、hrow new IOException(strReply.Substring(4); else trType = ttType; / / 获得传输模式 / / 传输模式 public TransferType GetTransferType() return trType; #endregion #region 文件操作 / / 获得文件和目录列表 / / 文件名的匹配字符串 / /*public FTPRecord List(string strMask) / 建立链接 if (!bConnected) Connect(); /建立进行数据连接的socket Socket socketDa

13、ta = CreateDataSocket(); /传送命令 SendCommand(NLST + strMask); if (iReplyCode = 550) Put(strMask); SendCommand(NLST + strMask); /分析应答代码 if (!(iReplyCode = 150 | iReplyCode = 125 | iReplyCode = 226) throw new IOException(strReply.Substring(4); /获得结果 strMsg = ; while (true) int iBytes = socketData.Receiv

14、e(buffer, buffer.Length, 0); strMsg += ASCII.GetString(buffer, 0, iBytes); if (iBytes buffer.Length) break; char seperator = r,n ; string strsFileList = strMsg.Split(seperator, StringSplitOptions.RemoveEmptyEntries); socketData.Close();/数据socket关闭时也会有返回码 if (iReplyCode != 226) ReadReply(); if (iRepl

15、yCode != 226) throw new IOException(strReply.Substring(4); return strsFileList; */ / / 获取文件大小 / / 文件名 / 文件大小 private long GetFileSize(string strFileName) if (!bConnected) Login(); SendCommand(SIZE + Path.GetFileName(strFileName); long lSize = 0; if (iReplyCode = 213) lSize = Int64.Parse(strReply.Sub

16、string(4); else throw new IOException(strReply.Substring(4); return lSize; / / 删除 / / 待删除文件名 public void Delete(string strFileName) if (!bConnected) Login(); SendCommand(DELE + strFileName); if (iReplyCode != 250) throw new IOException(strReply.Substring(4); / / 重命名(如果新文件名与已有文件重名,将覆盖已有文件) / / 旧文件名 /

17、 新文件名 public void Rename(string strOldFileName, string strNewFileName) if (!bConnected) Login(); SendCommand(RNFR + strOldFileName); if (iReplyCode != 350) throw new IOException(strReply.Substring(4); / 如果新文件名与原有文件重名,将覆盖原有文件 SendCommand(RNTO + strNewFileName); if (iReplyCode != 250) throw new IOExce

18、ption(strReply.Substring(4); #endregion #region 上传和下载 public void CancelOperation() _cancel = true; / / 下载一个文件 / / 要下载的文件名 / 本地目录(不得以结束) / 保存在本地时的文件名 public void DownloadFile(string strRemoteFileName, string strFolder, string strLocalFileName) _cancel = false; if (!bConnected) Login(); SetTransferTy

19、pe(TransferType.Binary); if (strLocalFileName.Equals() strLocalFileName = strRemoteFileName; if (!File.Exists(strLocalFileName) Stream st = File.Create(strLocalFileName); st.Close(); /取得源文件的大小 long total_size = GetFileSize(strRemoteFileName); FileStream output = new FileStream(strFolder + + strLocal

20、FileName, FileMode.Create); Socket socketData = CreateDataSocket(); SendCommand(RETR + strRemoteFileName); if (!(iReplyCode = 150 | iReplyCode = 125 | iReplyCode = 226 | iReplyCode = 250) throw new IOException(strReply.Substring(4); /已经下载过 int progressed = 0; while (!_cancel) int iBytes = socketData

21、.Receive(buffer, buffer.Length, 0); output.Write(buffer, 0, iBytes); if (iBytes = 0) break; progressed += iBytes; if (DownloadProgress != null) /下载进度 int percent = (int)(progressed / (double)total_size * 100); FTPProgressEventArgs e = new FTPProgressEventArgs(percent); e.TotalBytes = (int)total_size

22、; e.ProgressedBytes = progressed; DownloadProgress(this, e); output.Close(); if (socketData.Connected) socketData.Close(); if (!(iReplyCode = 226 | iReplyCode = 250) ReadReply(); if (!(iReplyCode = 226 | iReplyCode = 250) throw new IOException(strReply.Substring(4); / / 上传一个文件 / / 本地文件名 public void

23、UploadFile(List strFileNames) _cancel = false; if (!bConnected) Login(); foreach (string strFileName in strFileNames) Socket socketData = CreateDataSocket(); SendCommand(STOR + Path.GetFileName(strFileName); if (!(iReplyCode = 125 | iReplyCode = 150) throw new IOException(strReply.Substring(4); Socket _socket = socketData.Accept(); int msecs_passed = 0; /等待10秒钟 while (_socket.Available (10000 / 10) break; FileStream input = new FileStream(strFileName, FileMode.Ope

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

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1