FtpServer.cs
资源名称:FtpServer.rar [点击查看]
上传用户:xuelanruo
上传日期:2015-04-02
资源大小:163k
文件大小:6k
源码类别:
Ftp服务器
开发平台:
C#
- using System;
- using System.IO;
- using System.Data;
- using System.Collections;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Security.Permissions;
- using System.Configuration;
- namespace FtpServerLibrary
- {
- public class ConnectionEventArgs : EventArgs
- {
- string m_ip;
- public string Ip
- {
- get
- {
- return m_ip;
- }
- }
- public ConnectionEventArgs(string ip)
- {
- this.m_ip=ip;
- }
- }
- /// <summary>
- /// Code retour de lecture de commande
- /// </summary>
- public enum ReadReplyCode
- {
- Ok = 0,
- TimeOut = 1,
- UnknownError = 2
- }
- 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; }
- }
- }
- public class FtpServer
- {
- private TcpListener FTP_Listener = null;
- private Hashtable m_sessionList = null;
- private Hashtable m_sessionThreads = null;
- private Thread m_listeningThread = null;
- private bool m_listening = false;
- private long m_sessID = 0;
- #region 属性
- public static void SetConfigValue(string Key, string Value)
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- AppSettingsSection appSettings = config.AppSettings as AppSettingsSection;
- appSettings.Settings[Key].Value = Value;
- appSettings.SectionInformation.ForceSave = true;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- }
- public string IPAddress
- {
- get
- {
- try
- {
- return ConfigurationManager.AppSettings["IPAddress"];
- }
- catch
- {
- return "ALL";
- }
- }
- set
- {
- SetConfigValue("IPAddress", value);
- }
- }
- public int MaxBadCommands
- {
- get
- {
- try
- {
- return Convert.ToInt32(ConfigurationManager.AppSettings["MaxBadCommands"]);
- }
- catch
- {
- return 30;
- }
- }
- set
- {
- try
- {
- SetConfigValue("MaxBadCommands",((int)value).ToString());
- }
- catch
- {}
- }
- }
- public int Port
- {
- get
- {
- try
- {
- return Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
- }
- catch
- {
- return 21;
- }
- }
- set
- {
- try
- {
- SetConfigValue("Port",((int)value).ToString());
- }
- catch
- {
- }
- }
- }
- public int MaxThreads
- {
- get
- {
- try
- {
- return Convert.ToInt32(ConfigurationManager.AppSettings["MaxThreads"]);
- }
- catch
- {
- return 100;
- }
- }
- set
- {
- try
- {
- SetConfigValue("MaxThreads",((int)value).ToString());
- }
- catch
- {}
- }
- }
- public int SessionIdleTimeOut
- {
- get
- {
- try
- {
- return Convert.ToInt32(ConfigurationManager.AppSettings["SessionIdleTimeOut"]);
- }
- catch
- {
- return 800000;
- }
- }
- set
- {
- try
- {
- SetConfigValue("SessionIdleTimeOut",((int)value).ToString());
- }
- catch
- {}
- }
- }
- public int CommandIdleTimeOut
- {
- get
- {
- try
- {
- return Convert.ToInt32(ConfigurationManager.AppSettings["CommandIdleTimeOut"]);
- }
- catch
- {
- return 60000;
- }
- }
- set
- {
- try
- {
- SetConfigValue("CommandIdleTimeOut",((int)value).ToString());
- }
- catch
- {}
- }
- }
- #endregion
- #region 事件
- 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
- public FtpServer()
- {
- }
- public void Start()
- {
- try
- {
- if(!this.m_listening)
- {
- this.m_sessionList=new Hashtable();
- this.m_sessionThreads=new Hashtable();
- this.m_listeningThread=new Thread(new ThreadStart(Listen));
- this.m_listeningThread.Start();
- if(this.Started!=null)
- {
- Started(this,EventArgs.Empty);
- }
- }
- }
- catch{}
- }
- 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<m_sessID; i++)
- {
- this.RemoveSession(i);
- }
- //on d閏lenche l'関鑞ement Stopped
- if(this.Stopped!=null)
- {
- Stopped(this,EventArgs.Empty);
- }
- }
- }
- catch{}
- }
- private void Listen()
- {
- 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);
- }
- FTP_Listener.Start();
- this.m_listening=true;
- while(true)
- {
- if(m_sessionList.Count < this.MaxThreads)
- {
- Socket clientSocket = FTP_Listener.AcceptSocket();
- if(this.ClientConnected!=null)
- {
- this.ClientConnected(this,new ConnectionEventArgs(clientSocket.RemoteEndPoint.ToString()));
- }
- FtpSession session=new FtpSession(clientSocket,this,this.m_sessID);
- Thread clientThread = new Thread(new ThreadStart(session.StartProcessing));
- this.m_sessionList.Add(m_sessID,session);
- clientThread.Start();
- this.m_sessionThreads.Add(m_sessID,clientThread);
- m_sessID++;
- }
- else
- {
- Thread.Sleep(100);
- }
- }
- }
- public void RemoveSession(long sessID)
- {
- try
- {
- //Evenement ClientDisconnected
- if(this.ClientDisconected!=null)this.ClientDisconected(this,new ConnectionEventArgs(((FtpSession)this.m_sessionList[sessID]).RemoteEndPoint.ToString()));
- ((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{};
- }
- }
- }