■ FTP 서버 만들기

----------------------------------------------------------------------------------------------------


TestSolution.zip


[TestConsole 프로젝트]

 

Program.cs

 

 

using System;

 

using TestLibrary;

 

namespace TestConsole

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        static void Main()

        {

            FTPServer server = new FTPServer();

 

            server.LogHandler = new DefaultLogHandler(true);

            server.LocalPort  = 10000;

            server.BufferSize = 64 * 1024; // KB 단위

 

            Console.Out.WriteLine("FTP 서버가 시작되었습니다.");

 

            try

            {

                server.Run();

            }

            catch(Exception exception)

            {

                Console.Error.WriteLine(exception.Message);

            }

        }

 

        #endregion

    }

}

 

 

[TestLibrary 프로젝트]

 

FTPServer.cs

 

 

using System.Collections.Generic;

using System.Net;

using System.Net.Sockets;

 

namespace TestLibrary

{

    /// <summary>

    /// FTP 서버

    /// </summary>

    public class FTPServer

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 디폴트 버퍼 크기

        /// </summary>

        private const int DEFAULT_BUFFER_SIZE = 64 * 1024;

 

        /// <summary>

        /// 디폴트 포트

        /// </summary>

        private const int DEFAULT_PORT = 21;

 

 

        /// <summary>

        /// 지역 종점

        /// </summary>

        private IPEndPoint localEndPoint;

 

        /// <summary>

        /// TCP 리스너

        /// </summary>

        private TcpListener tcpListener = null;

 

        /// <summary>

        /// 세션 리스트

        /// </summary>

        private List<FTPSession> sessionList;

 

        /// <summary>

        /// 버퍼 크기

        /// </summary>

        private int bufferSize = DEFAULT_BUFFER_SIZE;

 

 

        /// <summary>

        /// 로그 핸들러

        /// </summary>

        private ILogHandler logHandler = null;

 

        /// <summary>

        /// 인증 핸들러

        /// </summary>

        private IAuthenticationHandler authenticationHandler = null;

 

        /// <summary>

        /// 파일 시스템 핸들러

        /// </summary>

        private IFileSystemHandler fileSystemHandler = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 지역 종점 - LocalEndPoint

 

        /// <summary>

        /// 지역 종점

        /// </summary>

        public IPEndPoint LocalEndPoint

        {

            get

            {

                return this.localEndPoint;

            }

            set

            {

                this.localEndPoint = value;

            }

        }

 

        #endregion

        #region 지역 주소 - LocalAddress

 

        /// <summary>

        /// 지역 주소

        /// </summary>

        public IPAddress LocalAddress

        {

            get

            {

                return this.localEndPoint.Address;

            }

            set

            {

                this.localEndPoint.Address = value;

            }

        }

 

        #endregion

        #region 지역 포트 - LocalPort

 

        /// <summary>

        /// 지역 포트

        /// </summary>

        public int LocalPort

        {

            get

            {

                return this.localEndPoint.Port;

            }

            set

            {

                this.localEndPoint.Port = value;

            }

        }

 

        #endregion

 

        #region 버퍼 크기 - BufferSize

 

        /// <summary>

        /// 버퍼 크기

        /// </summary>

        public int BufferSize

        {

            get

            {

                return this.bufferSize;

            }

            set

            {

                this.bufferSize = value;

            }

        }

 

        #endregion

 

        #region 로그 핸들러 - LogHandler

 

        /// <summary>

        /// 로그 핸들러

        /// </summary>

        public ILogHandler LogHandler

        {

            get

            {

                return this.logHandler;

            }

            set

            {

                this.logHandler = value;

            }

        }

 

        #endregion

        #region 인증 핸들러 - AuthenticationHandler

 

        /// <summary>

        /// 인증 핸들러

        /// </summary>

        public IAuthenticationHandler AuthenticationHandler

        {

            get

            {

                return this.authenticationHandler;

            }

            set

            {

                this.authenticationHandler = value;

            }

        }

 

        #endregion

        #region 파일 시스템 핸들러 - FileSystemHandler

 

        /// <summary>

        /// 파일 시스템 핸들러

        /// </summary>

        public IFileSystemHandler FileSystemHandler

        {

            get

            {

                return this.fileSystemHandler;

            }

            set

            {

                this.fileSystemHandler = value;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - FTPServer()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public FTPServer()

        {

            this.localEndPoint = new IPEndPoint(GetDefaultAddress(), DEFAULT_PORT);

            this.sessionList   = new List<FTPSession>();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 실행하기 - Run()

 

        /// <summary>

        /// 실행하기

        /// </summary>

        public void Run()

        {

            if(this.authenticationHandler == null)

            {

                this.authenticationHandler = new DefaultAuthenticationHandler();

            }

 

            if(this.fileSystemHandler == null)

            {

                this.fileSystemHandler = new DefaultFileSystemHandler();

            }

 

            if(this.tcpListener == null)

            {

                this.tcpListener = new TcpListener(localEndPoint);

            }

 

            this.tcpListener.Start();

 

            try

            {

                while(true)

                {

                    Socket peerSocket = this.tcpListener.AcceptSocket();

 

                    IPEndPoint peerPort = (IPEndPoint) peerSocket.RemoteEndPoint;

 

                    FTPSession session = new FTPSession

                    (

                        peerSocket,

                        bufferSize,

                        this.logHandler.Clone(peerPort),

                        this.authenticationHandler.Clone(peerPort),

                        this.fileSystemHandler.Clone(peerPort)

                    );

 

                    session.Start();

 

                    this.sessionList.Add(session);

 

                    for(int i = this.sessionList.Count - 1; i >= 0; i--)

                    {

                        if(!this.sessionList[i].IsOpen)

                        {

                            this.sessionList.RemoveAt(i);

 

                            i--;

                        }

                    }

                }

            }

            catch(SocketException)

            {

            }

            finally

            {

                foreach(FTPSession session in this.sessionList)

                {

                    session.Stop();

                }

            }

        }

 

        #endregion

        #region 중단하기 - Stop()

 

        /// <summary>

        /// 중단하기

        /// </summary>

        public void Stop()

        {

            this.tcpListener.Stop();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 디폴트 주소 구하기 - GetDefaultAddress()

 

        /// <summary>

        /// 디폴트 주소 구하기

        /// </summary>

        /// <returns>디폴트 주소</returns>

        private IPAddress GetDefaultAddress()

        {

#if WindowsCE

            IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());

 

            foreach(IPAddress ipAddress in ipHostEntry.AddressList)

            {

                if(ipAddress.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(ipAddress))

                {

                    return ipAddress;

                }

            }

 

            return IPAddress.Loopback;

#else

            return IPAddress.Any;

#endif

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요