Downloading a file from an ftp server

  • Thread starter =?ISO-8859-1?Q?Xaver_S=FCnkeler?=
  • Start date
?

=?ISO-8859-1?Q?Xaver_S=FCnkeler?=

Hello,
I'm trying to write some code to download a file from a ftp server and
I'm not quite sure, if I have understood the FTP-RFC.
Code goes like this:

m_socket = new Socket ("ftp:\\ftp.heise.de", 21);
m_inStream = m_socket.getInputStream();
m_outStream = m_socket.getOutputStream();
String request = "ftp ftp.heise.de\nUSER anonymous\nPASS guest\n
TYPE I\nPASV\n";

Up to here everything's going well; the connection gets established and
the last response from the server is what has to be expected:

227 Entering passive mode <193,99,144,79,122,164>

From the above line I extract the raw IP-address and put it in a byte
buffer named address, so that

rawIP[0] = 193
rawIP[1] = 99
rawIP[2] = 144
rawIP[3] = 79

I construct a portnumber by the last two numbers:

portnumber = 122 * 255 + 164;

Then I try to construct a new socket:

InetAddress addr = InetAddress.getByAddress(rawIP);
Socket dataSocket = new Socket (addr, portnumber);


At this point an exception is thrown:
java.net.Connectexception: connection refused: connect.

The problem is not prohibited access to the server.
The building of the InetAddress seems correct, because
addr.getHostAddress() delivers ftp.heise.de

I'm not quite sure, if the building of the portnumber is correct. Does
anybody know some helpful hints ?

Xaver
 
G

Gordon Beaton

I'm not quite sure, if the building of the portnumber is correct.
Does anybody know some helpful hints ?

The address and port number look ok.

I believe that you need to issue a command (e.g. LIST, STOR etc)
before attempting to connect.

Note too that the correct line ending is \r\n, even though some
servers might accept other variations.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top