Thread: How to connect to a ftp server

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    How to connect to a ftp server

    Hi,

    What I am trying to do is to connect to my ftp server where I will read and write to a file that is located like this:

    "Path\\This.txt";

    I have googled around for 2 days but I cant find a good example code on how to:

    * Connect to ftp.server
    * Call a Path on the server, to Read/Write

    I dont exactly know all steps but if I could find any good examples of this, it really would help me starting out this.

    The only code that I have found is something like this but I beleive this is for TCP wich is not the right thing:



    Code:
    using namespace System.Net.Sockets;
    
    TcpClient TcpCo = new TcpClient();
    TcpCo.Connect( "ftp.urladdress.com", 25 );

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    First of all, FTP is port 20/21, not 25. 25 is SMTP.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I googled for two seconds...

    FtpWebRequest Class (System.Net)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thank you, I will use port 20/21 and also look into: FtpWebRequest Class (System.Net) and see what I can start out with.
    Last edited by Coding; 05-07-2009 at 03:17 PM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I have succeded to connect to the ftp server and also the file that I want to read like this. That was really nice

    I also succeded to read the lines from the file. Then I am trying to write lines to the file but here I get an errormessage:

    Code:
    The stream is not writable
    However I have red something about EnableSsl = true

    "Unless the EnableSsl property is true, all data and commands, including your user name and password information, are sent to the server in clear text. Anyone monitoring network traffic can view your credentials and use them to connect to the server."

    The thing is that it will be users connecting to the ftp.server and what I am afraid for is that any "hackers" might see what "User" and "Password" there is. I dont really know so much about any securitys for this.


    Code:
    //First a request to FTP Server shall be initiated, requesting the file needed. 
     String^ FilePath = "ftp://myWebsite.info:21/MyFolder/MyFile.txt";
    
     FtpWebRequest^ objRequest = (FtpWebRequest^)WebRequest::Create(FilePath);
     objRequest->Credentials = gcnew NetworkCredential("User", "Password");			 
    
    
    
    //Then, we have to get the response for this request:
    FtpWebResponse ^objResponse = (FtpWebResponse^) objRequest->GetResponse();
    			 
    
    			 //Get the stream 
    			 Stream^ responseStream = objResponse->GetResponseStream();
    
    
    			 //read the file;
    			 List<String^> Lines = gcnew List<String^>();
    			 StreamReader^ reader = gcnew StreamReader(responseStream);
    
    			 while( reader->Peek() >= 0 )
    			 {
               			              Lines.Add(reader->ReadLine()); //Save all Lines;
            		 }
    			 //Lets close Everything
    			 reader->Close();
    
    
    
    
    
    			 //Write something to file for test 
    			 StreamWriter^ writer = gcnew StreamWriter(responseStream);
    
    			 for( int i = 0; i < 5; i++ ) 
    			 {
    				 writer->WriteLine("SomeLines");            //This gives an errormessage telling that the stream is not writable
    			 }
    			 writer->Close();
    			 responseStream->Close(); //close the stream after reading;
    			 objResponse->Close(); //Closes the connection to the server;
    Last edited by Coding; 05-07-2009 at 03:18 PM.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    :/

    Quote Originally Posted by Coding View Post
    I have succeded to connect to the ftp server and also the file that I want to read like this. That was really nice

    I also succeded to read the lines from the file. Then I am trying to write lines to the file but here I get an errormessage:

    Code:
    The stream is not writable
    However I have red something about EnableSsl = true

    "Unless the EnableSsl property is true, all data and commands, including your user name and password information, are sent to the server in clear text. Anyone monitoring network traffic can view your credentials and use them to connect to the server."

    The thing is that it will be users connecting to the ftp.server and what I am afraid for is that any "hackers" might see what "User" and "Password" there is. I dont really know so much about any securitys for this.


    Code:
    //First a request to FTP Server shall be initiated, requesting the file needed. 
     String^ FilePath = "ftp://myWebsite.info:21/MyFolder/MyFile.txt";
    
     FtpWebRequest^ objRequest = (FtpWebRequest^)WebRequest::Create(FilePath);
     objRequest->Credentials = gcnew NetworkCredential("User", "Password");			 
    
    
    
    //Then, we have to get the response for this request:
    FtpWebResponse ^objResponse = (FtpWebResponse^) objRequest->GetResponse();
    			 
    
    			 //Get the stream 
    			 Stream^ responseStream = objResponse->GetResponseStream();
    
    
    			 //read the file;
    			 List<String^> Lines = gcnew List<String^>();
    			 StreamReader^ reader = gcnew StreamReader(responseStream);
    
    			 while( reader->Peek() >= 0 )
    			 {
               			              Lines.Add(reader->ReadLine()); //Save all Lines;
            		 }
    			 //Lets close Everything
    			 reader->Close();
    
    
    
    
    
    			 //Write something to file for test 
    			 StreamWriter^ writer = gcnew StreamWriter(responseStream);
    
    			 for( int i = 0; i < 5; i++ ) 
    			 {
    				 writer->WriteLine("SomeLines");            //This gives an errormessage telling that the stream is not writable
    			 }
    			 writer->Close();
    			 responseStream->Close(); //close the stream after reading;
    			 objResponse->Close(); //Closes the connection to the server;
    I am having the same troubles as you, but when I try to implement that code it just gives me a load of undeclared identifier errors, can you think of any reasons why or how to fix them? I should note that this is the first BIG thing I've done in C++.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Are you sure you have write-access to the server? You may be able to read but not write.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to connect to an FTP server and download a file?
    By Comeback Kid in forum C Programming
    Replies: 1
    Last Post: 11-19-2005, 08:44 PM
  2. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  3. Visual Studio and FTP Server...
    By Grayson_Peddie in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-03-2003, 12:31 PM
  4. FTP Server :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2002, 07:14 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM