my dog learned polymorphism*
The moose likes Java in General and the fly likes java networking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java networking" Watch "java networking" New topic
Author

java networking

Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41
I am making a class that will use ftp to put files on a remote server.
I can connect to the server with no problem and run the help command
but when I try to run the list command I get an error message

220 FTP Server ready.
331 Password required for u35670060.
230 User u35670060 logged in.
214-The following commands are recognized (* =>'s unimplemented).
USER PASS ACCT* CWD XCWD CDUP XCUP SMNT*
QUIT REIN* PORT PASV TYPE STRU MODE RETR
STOR STOU APPE ALLO* REST RNFR RNTO ABOR
DELE MDTM RMD XRMD MKD XMKD PWD XPWD
SIZE LIST NLST SITE SYST STAT HELP NOOP
FEAT OPTS ADAT* AUTH* CCC* CONF* ENC* MIC*
PBSZ* PROT*
214 Direct comments to your support help desk.
257 "/" is current directory.
425 Unable to build data connection: Connection refused

checked the web and found that it may be a problem with my socket connection
below is the code.

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ftpClient{
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
String hostname = "www.server.com";


public ftpClient(){

runFTP();
}
public void runFTP(){

try{
socket = new Socket(hostname,21);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

String command = "user username\n";
out.print(command);

command = "pass password\n";
out.print(command);

String response = in.readLine();

System.out.println(response);
command = "help \n";
out.print(command);
command = "pwd \n";
out.print(command);
command = "list \n";
out.print(command);
command = "quit \n";
out.print(command);
out.flush();
reading();
out.flush();



}
catch(UnknownHostException e) {
System.out.println("Don't know about host: " + hostname);
}
catch(IOException e) {
System.out.println("Couldn't get I/O for the connection to: " + e);
}
out.close();
}
public void reading(){
try{
String line = null;
while ((line = in.readLine()) != null){

System.out.println(line );
}
in.close();

}
catch(IOException e) {
System.out.println("Couldn't get I/O for the connection to: " + e);

}
}
public static void main(String[] args){
ftpClient fc = new ftpClient();

}
}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Unless this is just an exercise, why are you reinventing the wheel by writing your own java FTP client? There are a number of open source Java clients availabe, as well as some API right within the JDK for accessing FTP servers.

Here's an article in Java World that explores some of the options.

http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html

Back to the original question: I suspect that the buffer containing information coming back from the server may be filling up before you read it. I'd suggest putting code to read the response after each command sent.


Merrill
Consultant, Sima Solutions
Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41
just an exercise,
I will give your suggestion a try
 
wood burning stoves
 
subject: java networking