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
Gordon Beaton - 28 May 2006 16:47 GMT
> 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

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
EJP - 29 May 2006 03:13 GMT
> I construct a portnumber by the last two numbers:
>
> portnumber = 122 * 255 + 164;
Multiply by 256 not 255.
Xaver Sünkeler - 30 May 2006 08:16 GMT
> Multiply by 256 not 255.
That's right. The worst mistakes are the stupid ones. Thanks for your help!
Xaver
Alex Hunsley - 29 May 2006 13:22 GMT
> 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:
Why reinvent the wheel?
http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html
I suppose you might be making another library or have some reason to
roll your own. But if you don't, save yourself the bother!
Xaver Sünkeler - 30 May 2006 08:17 GMT
> Why reinvent the wheel?
Good for learning.
Xaver