Reputation: 39
How do I implement retr and list commands in my program? I've looked on the internet but I didn't find anything that helped me. I also need to add the ports 20 and 21. My program is a FTP client and it has to receive files and replies from a FTP server.
import it.sauronsoftware.ftp4j.FTPClient;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class FTPKlient {
static String username;
static String pass;
static class Fil
{
Socket Klient;
static BufferedReader br;
static DataInputStream din;
static DataOutputStream dos;
Fil(Socket soc)
{
try
{
Klient=soc;
din=new DataInputStream(Klient.getInputStream());
dos=new DataOutputStream(Klient.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
}
catch(Exception s)
{
}
}
public static void main(String args[]) throws Exception
{
Scanner scan= new Scanner(System.in);
System.out.println("Hvad er brugernavnet?:");
username=scan.nextLine();
System.out.println("Hvad er koden?:");
pass=scan.nextLine();
FTPClient client = new FTPClient();
client.connect("ftp.host.com");
if(username.equals("geek") && (pass.equals("hello"))){
} else {
System.out.println("Adgang nægtet, du har skrevet de forkerte oplysninger");
}
Socket soc=new Socket("localhost",14147);
Fil t=new Fil(soc);
t.menu();
}
private static void Modtag() throws Exception
{
String filnavn;
System.out.print("Hvilken fil søger du? :");
filnavn=br.readLine();
dos.writeUTF(filnavn);
String msgFromServer=din.readUTF();
if(msgFromServer.compareTo("Filen findes ikke")==0)
{
System.out.println("Den indtastede fil findes ikke i serveren");
return;
}
else if(msgFromServer.compareTo("Færdig")==0)
{
System.out.println("Filen er i gang med at blive hentet, vent venligst");
File f=new File(filnavn);
if(f.exists())
{
String o;
System.out.println("Destinationen har allerede en fil med dette navn. Vil du erstatte filen? (J/N) ?");
o=br.readLine();
if (o.equals("N"))
{
dos.flush();
return;
}
}
FileOutputStream fot=new FileOutputStream(f);
int c;
String temp;
do
{
temp=din.readUTF();
c=Integer.parseInt(temp);
if(c!=-1)
{
fot.write(c);
}
}while(c!=-1);
fot.close();
System.out.println(din.readUTF());
}
}
public static void menu() throws Exception
{
while(true)
{
System.out.println("1. Modtag Fil");
System.out.println("2. Afslut");
System.out.print("\nHvad vil du foretage dig? :");
int vm;
vm=Integer.parseInt(br.readLine());
if(vm==1)
{
dos.writeUTF("GET");
Modtag();
}
else
{
dos.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}
}
Upvotes: 0
Views: 1260
Reputation: 46
Here's my advice:
First, learn how to FTP with the telnet command. You can see an example of this at https://www.webdigi.co.uk/blog/2009/ftp-using-raw-commands-and-telnet/
You see that you need two connections: one on port 21, to which you send FTP commands, and the second one, on the port that is returned in the response to the PASV command. The second connection will give you the listing, or the file downloaded.
Then you try to map "telnet" connection to "socket". They are similar, because they both open a TCP connection to a host on a specific port.
Upvotes: 1