您好,欢迎光临本网站![请登录][注册会员]  
文件名称: DotNetFtpSources
  所属分类: FTP
  开发工具:
  文件大小: 845kb
  下载次数: 0
  上传时间: 2010-08-06
  提 供 者: junbo_z*******
 详细说明: /* Ftp Dot Net Main File : contains Events Declarations, enumerations and FtpServer Class (the main class of the project) Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS F OR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Last modification : 04/20/2004 by Simon FERQUEL */ using System; using System.IO; using System.Data; using System.Collections; using System.Net; using System.Net.Sockets; using System.Threading; using Microsoft.Win32; using System.Security.Permissions; //Afin de lire et 閏rire des donn閑s dans la base de registre, on donne les droits ?l'assembly [assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, All = "HKEY_LOCAL_MACHINE")] namespace SharpFtpServer { /// /// Argument de l'関鑞ement "Connection" /// public class ConnectionEventArgs : EventArgs { string m_ip; public string Ip { get{return m_ip;} } public ConnectionEventArgs(string ip) { this.m_ip=ip; } } /// /// Code retour de lecture de commande /// public enum ReadReplyCode{ Ok =0, TimeOut =1, UnknownError =2} /// /// Exception lev閑 lors d'une erreur de lecture de commande /// public class ReadException : System.Exception { private ReadReplyCode m_ReadReplyCode; public ReadException(ReadReplyCode code,string message) : base(message) { m_ReadReplyCode = code; } public ReadReplyCode ReadReplyCode { get{ return m_ReadReplyCode; } } } /// /// La classe FtpServer est le module qui 閏oute sur le port ftp et qui cr閑 les instances des FtpSession /// quand un client se connecte /// public class FtpServer { //Cl?de registre contenant la Configuration du serveur ftp private RegistryKey m_configRk = Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("BluCorp").CreateSubKey("SharpFtpServer"); private TcpListener FTP_Listener = null; // Listener 閏outant les tentatives de connection des clients private Hashtable m_sessionList = null; // Collection contenant toutes les sessions en cours private Hashtable m_sessionThreads = null; private Thread m_listeningThread = null; // Thread repr閟entant la boucle d'閏oute private bool m_listening = false; // Le serveur 閏oute-t'il? private long m_sessID = 0; // Toutes ces propri閠閟 sont des acc鑣 simplifi閟 ?la base de registre. La plupart des param鑤res // sont modifiable sans red閙arrer le serveur. #region Propri閠閟 public string IPAddress { get{ try { return (string)m_configRk.GetValue("IPAddress"); } catch { return "ALL"; } } set{m_configRk.SetValue("IPAddress",value);} } public Language Language { get { string lang=(string)m_configRk.GetValue("Language","fr"); switch(lang.ToLower()) { case "fr" : return Language.fr; case "en" : return Language.en; default : return Language.fr; } } set { switch(value) { case Language.fr : m_configRk.SetValue("Language","fr"); break; case Language.en : m_configRk.SetValue("Language","en"); break; default : m_configRk.SetValue("Language","fr"); break; } } } public int MaxBadCommands { get { try { return Convert.ToInt32((string)m_configRk.GetValue("MaxBadCommands")); } catch { return 30; } } set { try { m_configRk.SetValue("MaxBadCommands",((int)value).ToString()); } catch {} } } public int Port { get { try { return Convert.ToInt32((string)m_configRk.GetValue("Port")); } catch { return 21; } } set { try { m_configRk.SetValue("Port",((int)value).ToString()); } catch {} } } public int MaxThreads { get { try { return Convert.ToInt32((string)m_configRk.GetValue("MaxThreads")); } catch { return 100; } } set { try { m_configRk.SetValue("MaxThreads",((int)value).ToString()); } catch {} } } public int SessionIdleTimeOut { get { try { return Convert.ToInt32((string)m_configRk.GetValue("SessionIdleTimeOut")); } catch { return 800000; } } set { try { m_configRk.SetValue("SessionIdleTimeOut",((int)value).ToString()); } catch {} } } public int CommandIdleTimeOut { get { try { return Convert.ToInt32((string)m_configRk.GetValue("CommandIdleTimeOut")); } catch { return 60000; } } set { try { m_configRk.SetValue("CommandIdleTimeOut",((int)value).ToString()); } catch {} } } #endregion #region Prise en charge des 関鑞ements public delegate void ConnectionEventHandler(object sender, ConnectionEventArgs e); public event EventHandler Started; public event EventHandler Stopped; public event ConnectionEventHandler ClientConnected; public event ConnectionEventHandler ClientDisconected; #endregion /// /// Constructeur par d閒aut /// public FtpServer() { } /// /// M閠hode permettant le d閙arrage du serveur (du thread d'閏oute) /// public void Start() { try { //Si le serveur n'閏oute pas d閖?.. if(!this.m_listening) { //on r閕nitialise la collection des sessions en cours. this.m_sessionList=new Hashtable(); //ainsi que celle des threads de session this.m_sessionThreads=new Hashtable(); //on initialise le thread this.m_listeningThread=new Thread(new ThreadStart(Listen)); //et on d閙arre la boucle this.m_listeningThread.Start(); //On d閏lenche aussi l'関鑞ement "Started" if(this.Started!=null) { Started(this,EventArgs.Empty); } } } catch{} } /// /// M閠hode d'arr阾 du thread d'閏oute /// public void Stop() { try { //on ne stop le thread que si il est en train de tourner if(this.m_listening) { //on stop le thread this.m_listeningThread.Abort(); //on change la variable en cons閝uence this.m_listening=false; //on stop le listener FTP_Listener.Stop(); //et on le d閠ruit FTP_Listener=null; //On arr阾e aussi toutes les sessions en cours for (int i=0; i /// Boucle d'閏oute /// private void Listen() { //Param鑤rage du TcpListener if(this.IPAddress.ToLower().IndexOf("all") > -1) { FTP_Listener = new TcpListener(System.Net.IPAddress.Any,this.Port); } else { FTP_Listener = new TcpListener(System.Net.IPAddress.Parse(this.IPAddress),this.Port); } //D閙arrage de l'閏oute FTP_Listener.Start(); this.m_listening=true; while(true) { // On v閞ifie d'abord que le nombre dee connections maximum n'a pas 閠?atteind. if(m_sessionList.Count < this.MaxThreads) { // On attend une connection d'un client Socket clientSocket = FTP_Listener.AcceptSocket(); //on d閏lenche l'関鑞ement ClientConnected if(this.ClientConnected!=null) { this.ClientConnected(this,new ConnectionEventArgs(clientSocket.RemoteEndPoint.ToString())); } //on cr閑 la session FtpSession session=new FtpSession(clientSocket,this,this.m_sessID); //on cr閑 le thread qui va traiter les commandes pass閑s par le client Thread clientThread = new Thread(new ThreadStart(session.StartProcessing)); // Ajout de la session ?la collection des sessions en cours. this.m_sessionList.Add(m_sessID,session); // D閎ut du traitement clientThread.Start(); // Ajout du thread ?la collection de thread de session this.m_sessionThreads.Add(m_sessID,clientThread); m_sessID++; } else { Thread.Sleep(100); } } } /// /// Suppression d'une session de la liste des sessions actives /// /// Session ?supprimer public void RemoveSession(long sessID){ try { //Evenement ClientDisconnected if(this.ClientDisconected!=null)this.ClientDisconected(this,new ConnectionEventArgs(((FtpSession)this.m_sessionList[sessID]).RemoteEndPoint.ToString())); //Arr阾 du Thread correspondant ((Thread)this.m_sessionThreads[sessID]).Abort(); //suppression du thread this.m_sessionThreads.Remove(sessID); //Cloture de la session ((FtpSession)this.m_sessionList[sessID]).Close(); //suppression de la session this.m_sessionList[sessID]=null; this.m_sessionList.Remove(sessID); } catch{}; } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: Ftp源码 Demo C#
 输入关键字,在本站1000多万海量源码库中尽情搜索: