Click to See Complete Forum and Search --> : Want to make a FTP connection


Jonathan
March 30th, 1999, 10:08 AM
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

Ashish
May 14th, 1999, 03:07 PM
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

Patrick Liechty
May 14th, 1999, 03:52 PM
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