Ejemplo n.º 1
0
        public FTPConnection Connect()
        {
            FTPConnection ftpConnection = new FTPConnection();

            try
            {
                ftpConnection.ServerAddress = serverName;
                ftpConnection.ServerPort    = serverPort;
                ftpConnection.UserName      = (userName == string.Empty) ? "anonymous" : userName;
                ftpConnection.Password      = (userName == string.Empty) ? "1" : userPassword;
                ftpConnection.ConnectMode   = (passivMode) ? FTPConnectMode.PASV : FTPConnectMode.ACTIVE;
                ftpConnection.Connect();
                return(ftpConnection);
            }
            catch (FTPException)
            {
                CloseConnection(ftpConnection);
                throw;
            }
            catch (IOException)
            {
                CloseConnection(ftpConnection);
                throw;
            }
            catch (SocketException)
            {
                CloseConnection(ftpConnection);
                throw;
            }
        }
Ejemplo n.º 2
0
        private long connectionCount = 0;  // Note: 用于Connection命名,ConnectionName目前只用于日志

        private FTPConnection CreateConnection(ServerInfo server, bool setNotifier = false, string connectionName = null)
        {
            if (connectionName == null)
            {
                connectionCount++; connectionName = connectionCount.ToString();
            }
            int retryCount = 0;

            while (retryCount <= 3)
            {
                try
                {
                    retryCount++;
                    FTPConnection ftpConnection = new FTPConnection(connectionName, server);
                    if (setNotifier)
                    {
                        ftpConnection.FTPInfoNotifier += FTPInfoNotifier;
                    }
                    ftpConnection.Connect();
                    return(ftpConnection);
                }
                catch (FTPResponseException ex)
                {
                    if (!ex.Recoverable)
                    {
                        throw;
                    }
                }
            }
            throw new FTPResponseException("客户端无法创建Socket,请检查端口号使用情况");
        }