예제 #1
0
        /// <summary>
        /// Get a list of all the files in the current working directory from the server.
        /// </summary>
        /// <returns>A server message in response to this action.</returns>
        private ServerMessage Dir()
        {
            ServerMessage reply;

            if (CliMode == ClientMode.Passive)
            {
                SendCmd("PASV");
                reply = cmdCon.ReadMessage();
                // if successful, this will initiate a new data connection
                HandleReply(reply);

                if (!dataCon.IsConnected())
                {
                    Console.Error.WriteLine("No connection could be established to retrieve the directory listing.");
                    return(reply);
                }

                SendCmd("LIST");
                reply = cmdCon.ReadMessage();
                HandleReply(reply);

                Console.Write(dataCon.ReadDirectory());

                return(cmdCon.ReadMessage());
            }
            else
            {
                using (FTPListener dirCon = new FTPListener())
                {
                    String commaSepIP = String.Join(",", dirCon.LocalIP.ToString().Split('.'));
                    int    octet1     = dirCon.Port / 256;
                    int    octet2     = dirCon.Port % 256;
                    String ipParam    = String.Format("{0},{1},{2}", commaSepIP, octet1, octet2);

                    SendCmd("PORT", ipParam);
                    reply = cmdCon.ReadMessage();
                    if (reply.Code != SUCCESS)
                    {
                        return(reply);
                    }
                    HandleReply(reply);

                    SendCmd("LIST");
                    reply = cmdCon.ReadMessage();
                    HandleReply(reply);

                    Console.Write(dirCon.AcceptDirectory());
                }

                return(cmdCon.ReadMessage());
            }
        }
예제 #2
0
        /// <summary>
        /// Get a file from the server.
        /// </summary>
        /// <param name="fileName">The name of the file to request.</param>
        /// <returns>A server message in response to this action.</returns>
        private ServerMessage GetFile(String fileName)
        {
            ServerMessage reply;

            if (CliMode == ClientMode.Passive)
            {
                SendCmd("PASV");
                reply = cmdCon.ReadMessage();
                // if successful, this will initiate a new data connection
                HandleReply(reply);

                if (dataCon == null || !dataCon.IsConnected())
                {
                    Console.Error.WriteLine("No connection could be established to retrieve the file.");
                    return(reply);
                }

                SendCmd("RETR", fileName);
                reply = cmdCon.ReadMessage();
                HandleReply(reply);

                if (dataCon == null || !dataCon.IsConnected())
                {
                    return(new ServerMessage());
                }

                using (FileStream f = File.Create(fileName))
                {
                    dataCon.ReadFile(f, DatMode);
                }


                return(cmdCon.ReadMessage());
            }
            else
            {
                using (FTPListener fileCon = new FTPListener())
                {
                    String commaSepIP = String.Join(",", fileCon.LocalIP.ToString().Split('.'));
                    int    octet1     = fileCon.Port / 256;
                    int    octet2     = fileCon.Port % 256;
                    String ipParam    = String.Format("{0},{1},{2}", commaSepIP, octet1, octet2);

                    SendCmd("PORT", ipParam);
                    reply = cmdCon.ReadMessage();
                    if (reply.Code != SUCCESS)
                    {
                        return(reply);
                    }
                    HandleReply(reply);

                    SendCmd("RETR", fileName);
                    reply = cmdCon.ReadMessage();
                    HandleReply(reply);

                    if (reply.Code == PERMISSION_DENIED)
                    {
                        return(new ServerMessage());
                    }

                    using (FileStream f = File.Create(fileName))
                    {
                        fileCon.AcceptFile(f, DatMode);
                    }
                }

                return(cmdCon.ReadMessage());
            }
        }