I got stuck with FTP client java implementation. All I have to do is to connect to the FTP server and transfer one file to the server.
I managed to make a connection with the ftp server (see code below) and after that I entered passive mode with PASV command. Now I don't know what to do next. I tried to make a new socket after PASV command and to connect it to the FTP server port 20 but that didn't work.
My question is how to initiate file transfer when a connection is made? (My idea is to make a connection with port 20 and to execute STOR command, but I don't know how to do it).
Do you have any ideas or helpful advice?
btw. I have to implement this without using java classes like FTPClient
Here's my code:
public class FTPtest {
Socket socket;
PrintWriter pw;
BufferedReader input;
String info = "";
public FTPtest(){
try{
socket = new Socket("some_ftp_server", 21);
logUsername();
closeEverything();
}
catch(IOException ioe){
System.out.println("error");
}
}
public void logUsername()throws IOException{
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
//user
pw.write("USER some_user\n");
pw.flush();
System.out.println(input.readLine());
//pass
pw.write("PASS some_pass");
pw.flush();
System.out.println(input.readLine());
System.out.println(input.readLine());
//PASV
pw.write("PASV");
pw.flush();
}
public void closeEverything() throws IOException{
input.close();
pw.close();
socket.close();
}
public static void main(String[]args){
new FTPtest();
}
}