CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    26

    Want to make a FTP connection



    I have to do an FTP connection and i don't know how


    Here my code


    URL u= new URL("ftp", "bd1.netc.net", -1, "/qwerty/joe/donnees/question.txt");

    URLConnection c= u.openConnection();


    After that i don't know what to do

    I've try to open a stream to read the file but it throws me an exception (password). I don't know where i can set the password


    Please help me.


    thanks in advanced and sorry for my english



  2. #2
    Join Date
    May 1999
    Posts
    10

    Re: Want to make a FTP connection

    Hi,
    Basically for an FTP connection you need to create two sockets, one control and the other data socket.
    1. Creating a control socket:
    Socket control = new Socket( address, 21 );
    For reading and writing you need to define streams like this :
    BufferedReader input = new BufferedReader(
    new InputStreamReader(control.getInputStream())
    );
    PrintWriter output = new PrintWriter( control.getOutputStream(), true );

    So, now you can read from the socket using:
    System.input.readLine();
    and write using:
    output.println( String );
    ( I recommend you to read "Internet Programming" by Kris Jamsa for the FTP reply codes )

    2. Next create a data socket class for transferring files, listing dir. etc.

    public class DataSocket
    {
    Socket dataSocket;
    ServerSocket dServer;


    public DataSocket( ControlSocket control )
    throws IOException
    {
    int dataPort;
    dServer = new ServerSocket( 0, 5 );
    dataPort = dServer.getLocalPort();
    control.setPort( dataPort );
    }

    public void accept()
    throws IOException
    {
    dataSocket = dServer.accept();
    dServer.close();
    }

    public InputStream getInputStream()
    throws IOException
    {
    return dataSocket.getInputStream();
    }

    public OutputStream getOutputStream()
    throws IOException
    {
    return dataSocket.getOutputStream();
    }

    public void close()
    throws IOException
    {
    dataSocket.close();
    }
    }

    Hope this helps.

    - Ashish


  3. #3

    Re: Want to make a FTP connection

    You could just download an already written free library of network components. The components include an ftp component that makes ftp easy. I use it and it is well done. The web site is http://www.oroinc.com. Good luck

    Patrick Liechty
    http://www.devseek.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured