Exemple #1
0
 /// <summary>
 /// 没有ID的构造函数,用于添加操作
 /// </summary>
 /// <param name="address"></param>
 /// <param name="port"></param>
 /// <param name="username"></param>
 /// <param name="password"></param>
 /// <param name="literal"></param>
 public FTPServer(string address, int port, string username, string password, string literal, FTPServerType type)
 {
     _address       = address;
     _port          = port;
     _username      = username;
     _password      = password;
     _literal       = literal;
     _ftpservertype = type;
     Init();
 }
Exemple #2
0
        /// <summary>
        /// Disconnect from the FTP server
        /// </summary>
        public void Disconnect()
        {
            if (m_cmdsocket != null)
            {
                FTPResponse response = SendCommand("QUIT");
                m_cmdsocket.Close();
                m_cmdsocket = null;
            }

            m_server    = FTPServerType.Unknown;
            m_connected = false;
        }
Exemple #3
0
 /// <summary>
 /// 没有ID的构造函数,用于添加操作
 /// </summary>
 /// <param name="address"></param>
 /// <param name="port"></param>
 /// <param name="username"></param>
 /// <param name="password"></param>
 /// <param name="literal"></param>
 public FTPServer(string address, int port, string username, string password, string literal, FTPServerType type)
 {
     _address = address;
     _port = port;
     _username = username;
     _password = password;
     _literal = literal;
     _ftpservertype = type;
     Init();            
 }
Exemple #4
0
        /// <summary>
        /// Disconnect from the FTP server
        /// </summary>
        public void Disconnect()
        {
            if(m_cmdsocket != null)
            {
                FTPResponse response = SendCommand("QUIT");
                m_cmdsocket.Close();
                m_cmdsocket = null;
            }

            m_server	= FTPServerType.Unknown;
            m_connected = false;
        }
Exemple #5
0
        private void ConnectThread()
        {
            IPEndPoint	endpoint;
            FTPResponse response;

            m_cmdsocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

            try

            {
                IPAddress address=IPAddress.Parse(m_host);
                endpoint = new IPEndPoint(address, m_port);
            }

            catch(System.FormatException )
            {
                try
                {
                    IPAddress address = Dns.Resolve(m_host).AddressList[0];
                    endpoint = new IPEndPoint(address, m_port);
                }
                catch(SocketException)
                {
                    return;
                }

            }

            // make the connection
            try
            {
                m_cmdsocket.Connect(endpoint);
            }
            catch(Exception)
            {
                return;
            }

            // check the result
            response = ReadResponse();

            if(response.ID != 220)
            {
                m_cmdsocket.Close();
                m_cmdsocket = null;

                if(!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // set the user id
            response = SendCommand("USER " + m_uid, false);

            // check the response
            if( !((response.ID == 331) || (response.ID == 230)) )
            {
                m_cmdsocket.Close();
                m_cmdsocket=null;
                Disconnect();

                if(!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // if a PWD is required, send it
            if( response.ID == 331 )
            {
                response = SendCommand("PASS " + m_pwd, false);
                if( !((response.ID == 202) || (response.ID == 230)) )
                {
                    m_cmdsocket.Close();
                    m_cmdsocket=null;
                    Disconnect();
                    m_connected = false;
                    return;
                }
            }

            // get the server type
            response = SendCommand("SYST", false);
            if(response.Text.ToUpper().IndexOf("UNIX") > -1)
            {
                m_server = FTPServerType.Unix;
            }
            else if(response.Text.ToUpper().IndexOf("WINDOWS") > -1)
            {
                m_server = FTPServerType.Windows;
            }
            else
            {
                m_server = FTPServerType.Unknown;
            }

            m_connected = true;

            foreach (FTPConnectedHandler fh in Connected.GetInvocationList())
            {
                fh(this);
            }
        }
Exemple #6
0
        private void ConnectThread()
        {
            IPEndPoint  endpoint;
            FTPResponse response;

            m_cmdsocket = new Socket(AddressFamily.InterNetwork,
                                     SocketType.Stream,
                                     ProtocolType.Tcp);

            try

            {
                IPAddress address = IPAddress.Parse(m_host);
                endpoint = new IPEndPoint(address, m_port);
            }

            catch (System.FormatException)
            {
                try
                {
                    IPAddress address = Dns.GetHostEntry(m_host).AddressList[0];
                    //IPAddress address = Dns.Resolve(m_host).AddressList[0];
                    endpoint = new IPEndPoint(address, m_port);
                }
                catch (SocketException)
                {
                    return;
                }
            }

            // make the connection
            try
            {
                m_cmdsocket.Connect(endpoint);
            }
            catch (Exception)
            {
                return;
            }

            // check the result
            response = ReadResponse();

            if (response.ID != 220)
            {
                m_cmdsocket.Close();
                m_cmdsocket = null;

                if (!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // set the user id
            response = SendCommand("USER " + m_uid, false);

            // check the response
            if (!((response.ID == 331) || (response.ID == 230)))
            {
                m_cmdsocket.Close();
                m_cmdsocket = null;
                Disconnect();

                if (!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // if a PWD is required, send it
            if (response.ID == 331)
            {
                response = SendCommand("PASS " + m_pwd, false);
                if (!((response.ID == 202) || (response.ID == 230)))
                {
                    m_cmdsocket.Close();
                    m_cmdsocket = null;
                    Disconnect();
                    m_connected = false;
                    return;
                }
            }

            // get the server type
            response = SendCommand("SYST", false);
            if (response.Text.ToUpper().IndexOf("UNIX") > -1)
            {
                m_server = FTPServerType.Unix;
            }
            else if (response.Text.ToUpper().IndexOf("WINDOWS") > -1)
            {
                m_server = FTPServerType.Windows;
            }
            else
            {
                m_server = FTPServerType.Unknown;
            }

            m_connected = true;

            if (Connected != null)
            {
                foreach (FTPConnectedHandler fh in Connected.GetInvocationList())
                {
                    fh(this);
                }
            }
        }
Exemple #7
0
        private void ConnectThread()
        {
            IPEndPoint	endpoint;
            FTPResponse response;

            m_cmdsocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

            try

            {
                IPAddress address=IPAddress.Parse(m_host);
                endpoint = new IPEndPoint(address, m_port);
            }

            catch(System.FormatException )
            {
                try
                {
                    IPAddress address = Dns.Resolve(m_host).AddressList[0];
                    endpoint = new IPEndPoint(address, m_port);
                }
                catch(SocketException)
                {
                    return;
                }

            }

            // make the connection
            try
            {
                m_cmdsocket.Connect(endpoint);
            }
            catch(Exception ee)
            {
                System.Diagnostics.Debug.WriteLine("Exception in ConnectThread: " + ee.Message);
                return;
            }

            // check the result
            response = ReadResponse();

            if(response.ID != StatusCode.ServiceReady)
            {
                m_cmdsocket.Close();
                m_cmdsocket = null;

                if(!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // set the user id
            response = SendCommand("USER " + m_uid, false);

            // check the response
            if (!((response.ID == StatusCode.NameAccepted) || (response.ID == StatusCode.LoginSuccess)))
            {
                // m_cmdsocket.Close();
                // m_cmdsocket=null;
                Disconnect();

                if(!m_exceptions)
                {
                    return;
                }
                else
                {
                    m_connected = false;
                    return;
                }
            }

            // if a PWD is required, send it
            if (response.ID == StatusCode.NameAccepted)
            {
                response = SendCommand("PASS " + m_pwd, false);
                if (!((response.ID == StatusCode.CommandNotImplemented) || (response.ID == StatusCode.LoginSuccess)))
                {
                    // m_cmdsocket.Close();
                    // m_cmdsocket=null;
                    Disconnect();
                    // m_connected = false;
                    return;
                }
            }

            // get the server type
            response = SendCommand("SYST", false);
            if(response.Text.ToUpper().IndexOf("UNIX") > -1)
            {
                m_server = FTPServerType.Unix;
            }
            else if(response.Text.ToUpper().IndexOf("WINDOWS") > -1)
            {
                m_server = FTPServerType.Windows;
            }
            else
            {
                m_server = FTPServerType.Unknown;
            }

            m_connected = true;

            //foreach (FTPConnectedHandler fh in Connected.GetInvocationList())
            //{
            //    fh(this);
            //}
            if (Connected != null)
            {
                Connected(this);
            }
        }
Exemple #8
0
        /// <summary>
        /// Connect to the FTP server using the supplied username and password
        /// </summary>
        /// <param name="username">Username</param>
        /// <param name="password">Password</param>
        public void Connect(string username, string password)
        {
            m_uid = username;
            m_pwd = password;

            if (m_connected)
            {
                throw new FTPException("Already Connected");
            }

            try
            {
                IPEndPoint  endpoint;
                FTPResponse response;

                m_cmdsocket = new Socket(AddressFamily.InterNetwork,
                                         SocketType.Stream,
                                         ProtocolType.Tcp);

                try
                {
                    IPAddress address = IPAddress.Parse(m_host);
                    endpoint = new IPEndPoint(address, m_port);
                }
                catch (System.FormatException)
                {
                    try
                    {
                        IPAddress address = Dns.GetHostEntry(m_host).AddressList[0];
                        endpoint = new IPEndPoint(address, m_port);
                    }
                    catch (SocketException)
                    {
                        m_cmdsocket = null;
                        m_connected = false;
                        return;
                    }
                }

                // make the connection
                FTPParameters.EndConnectEvent = new AutoResetEvent(false);
                m_cmdsocket.BeginConnect(endpoint, EndConnection, m_cmdsocket);
                FTPParameters.EndConnectEvent.WaitOne(m_timeout, false);

                if (!m_cmdsocket.Connected)
                {
                    m_cmdsocket.Close();
                    return;
                }

                // check the result
                ReadResponse();

                if (m_response.ID != StatusCode.ServiceReady)
                {
                    m_cmdsocket.Close();
                    m_cmdsocket = null;

                    if (!m_exceptions)
                    {
                        return;
                    }
                    else
                    {
                        m_connected = false;
                        return;
                    }
                }

                // set the user id
                response = SendCommand("USER " + m_uid, false);

                // check the response
                if (!((response.ID == StatusCode.NameAccepted) || (response.ID == StatusCode.LoginSuccess)))
                {
                    m_cmdsocket.Close();
                    m_cmdsocket = null;
                    Disconnect();

                    if (!m_exceptions)
                    {
                        return;
                    }
                    else
                    {
                        m_connected = false;
                        return;
                    }
                }

                // if a PWD is required, send it
                if (response.ID == StatusCode.NameAccepted)
                {
                    response = SendCommand("PASS " + m_pwd, false);
                    if (!((response.ID == StatusCode.CommandNotImplemented) || (response.ID == StatusCode.LoginSuccess)))
                    {
                        m_cmdsocket.Close();
                        m_cmdsocket = null;
                        Disconnect();
                        m_connected = false;
                        return;
                    }
                }

                m_server    = FTPServerType.Unix;
                m_connected = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }