示例#1
0
        /// <summary>
        /// Construct an FTP client to connect to the given remote.
        /// </summary>
        /// <param name="remote"></param>
        public FTPClient(String remote)
        {
            try
            {
                cmdCon  = new FTPConnection(remote);
                dataCon = null;
                IsOpen  = true;
                IsDebug = false;
                CliMode = ClientMode.Passive;
                DatMode = DataMode.Binary;

                ServerMessage initialMsg = cmdCon.ReadMessage();
                HandleReply(initialMsg);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                IsOpen = false;
            }
        }
示例#2
0
        /// <summary>
        /// Open a data connection (a la passive mode) on the remote to transfer files (or directory info).
        /// This will extract the IP address and port number from a server message.
        /// </summary>
        /// <param name="msg">The server message that provoked opening a new data connection.</param>
        private void SetDataConnection(ServerMessage msg)
        {
            if (msg.Code != PASSIVE_MODE)
            {
                Console.Error.Write(
                    "Attempted to open remote data connection in passive mode.\n{0}",
                    msg
                    );
                return;
            }

            if (dataCon != null && dataCon.IsConnected())
            {
                dataCon.Close();
            }


            String targetRegex = @"(\d{1,3},){5}\d{1,3}";
            Match  m           = Regex.Match(msg.Text, targetRegex);

            if (!m.Success)
            {
                Console.Error.Write(
                    "Attempted to open remote data connection but server seems to have given no connection information:\n{0}",
                    msg
                    );
                return;
            }

            String target = m.Value;

            String[] byteStrs = target.Split(',');
            String   ipStr    = String.Join <String>(".", byteStrs.Take <String>(4));

            System.Net.IPAddress IP = System.Net.IPAddress.Parse(ipStr);

            String[] octetStrs = { byteStrs[byteStrs.Length - 2], byteStrs[byteStrs.Length - 1] };
            int      port      = (Int32.Parse(octetStrs[0]) * 256) + Int32.Parse(octetStrs[1]);

            dataCon = new FTPConnection(IP, port);
        }