C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 137 を表示中)
■80562 / )  C#でのFTPサーバ構築
□投稿者/ NiN_初心者 (1回)-(2016/07/27(Wed) 17:20:51)

分類:[C#] 

お世話になります。
プログラムを始めたばかりなのですが、サーバサンプルを仕事でつくることになりました。
1からは作れない為Webでソースコードを入手しデバッグまではできたのですが、クライアント側からホスト指定する際
以下のコードからどうホスト名を指定していいかわかりません。関係ありそうな所を張りました。もしよろしければご教授頂きたいです。

何卒宜しくお願いします。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;

using log4net;

namespace SharpFtpServer
{
    public class FtpServer : IDisposable
    {
        ILog _log = LogManager.GetLogger(typeof(FtpServer));

        private bool _disposed = false;
        private bool _listening = false;

        private TcpListener _listener;
        private List<ClientConnection> _activeConnections;

        private IPEndPoint _localEndPoint;

       
        public FtpServer()
            : this(IPAddress.Any, 21)
        {

        }

        public FtpServer(IPAddress ipAddress, int port)
        {
            _localEndPoint = new IPEndPoint(ipAddress, port);
        }

        public void Start()
        {
            _listener = new TcpListener(_localEndPoint);

            _log.Info("#Version: 1.0");
            _log.Info("#Fields: date time c-ip c-port cs-username cs-method cs-uri-stem sc-status sc-bytes cs-bytes s-name s-port");

            _listening = true;
            _listener.Start();

            _activeConnections = new List<ClientConnection>();

            _listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
        }

        public void Stop()
        {
            _log.Info("Stopping FtpServer");

            _listening = false;
            _listener.Stop();

            _listener = null;
        }

        private void HandleAcceptTcpClient(IAsyncResult result)
        {
            if (_listening)
            {
                _listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);

                TcpClient client = _listener.EndAcceptTcpClient(result);

                ClientConnection connection = new ClientConnection(client);

                _activeConnections.Add(connection);

                ThreadPool.QueueUserWorkItem(connection.HandleClient, client);
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    Stop();

                    foreach (ClientConnection conn in _activeConnections)
                    {
                        conn.Dispose();
                    }
                }
            }

            _disposed = true;
        }
    }
}

返信 編集キー/


管理者用

- Child Tree -