■ FTP 서버를 만드는 방법을 보여준다.
[TestConsole 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
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 } } |