Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Downloading a file from an ftp server

Thread view: 
Xaver Sünkeler - 28 May 2006 16:18 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:

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.