Skip to content
Snippets Groups Projects
Commit 7e7053fa authored by Samy Meghari's avatar Samy Meghari
Browse files

debut du parcours en profondeur

parent 63e8cc1c
Branches
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>sr1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
......@@ -12,7 +12,9 @@ public class App
public static void main( String[] args ) throws UnknownHostException, IOException
{
ClientFTP myclient = new ClientFTP("anonymous", "");
myclient.init("ftp.ubuntu.com", 21);
Client2FTP myclient2 = new Client2FTP(myclient);
myclient2.init("ftp.free.fr", 21);
myclient2.myExplorer("/","");
}
}
package sr1;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class Client2FTP {
private Socket mysocket;
private ClientFTP myclient;
public Client2FTP(ClientFTP myclient) {
this.myclient = myclient;
}
public void init(String ip,int port)throws UnknownHostException, IOException {
mysocket = new Socket(ip,port);
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
InputStream in = mysocket.getInputStream();
InputStreamReader isr= new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
String content = reader.readLine();
System.out.println(content);
printer.println("USER "+ this.myclient.getUser().trim()+"\r\n");
content = reader.readLine();
System.out.println(content);
printer.println("PASS "+this.myclient.getPassword().trim()+"\r\n");
content = reader.readLine();
System.out.println(content);
}
public void myExplorer(String dir,String space) throws IOException {
List<String> myFolder = new ArrayList<>();
myFolder = displaymyFolder(dir);
System.out.println("");
System.out.println("Display the first folders : ");
space+=" ";
for (String sd : myFolder) {
System.out.println(space+"└──"+sd);
}
}
public List<String> displaymyFolder(String myDir) throws IOException {
List<String> myFolder = new ArrayList<>();
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
InputStream in = mysocket.getInputStream();
InputStreamReader isr= new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
printer.println("CWD "+myDir+"\r\n");
reader.readLine();
printer.println("PWD "+"\r\n");
reader.readLine();
printer.println("PASV "+"\r\n");
String res = "";
int newport = 0;
String newip = null;
res= reader.readLine();
if(res.toLowerCase().startsWith("227 entering passive mode")) {
String temp = res.substring(res.indexOf("(")+1,res.indexOf(")"));
newip = getIP(temp);
newport = getPort(temp);
myFolder = getData(newip, newport);
}
res= reader.readLine();
return myFolder;
}
private List<String> getData(String ip,int newport) throws UnknownHostException, IOException {
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
printer.println("NLST "+"\r\n");
Socket mySocket;
List<String> myFolder = new ArrayList<>();
mySocket = new Socket(ip,newport);
InputStream in = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader reader2 = new BufferedReader(isr);
String dir;
while((dir = reader2.readLine())!=null){
myFolder.add(dir);
}
mySocket.close();
return myFolder;
}
public String getIP(String ip) {
String[] s = ip.split(",");
return s[0]+ "." + s[1]+ "." + s[2]+ "." + s[3];
}
public int getPort(String port) {
String[] s = port.split(",");
int i = Integer.parseInt(s[4])*256 + Integer.parseInt(s[5]);
return i;
}
}
......@@ -18,32 +18,51 @@ public class ClientFTP {
this.user=user;
this.password=password;
}
public ClientFTP() {
this.user="anonymous";
this.password= "";
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getIP(String ip) {
String[] s = ip.split(",");
return s[0]+ "." + s[1]+ "." + s[2]+ "." + s[3];
}
public int getPort(String port) {
String[] s = port.split(",");
int i = Integer.parseInt(s[4])*256 + Integer.parseInt(s[5]);
return i;
}
public void init(String ip, int port) throws UnknownHostException, IOException {
this.mysocket = new Socket(ip,port);
OutputStream out = mysocket.getOutputStream();
PrintWriter printer = new PrintWriter(out, true);
InputStream in = mysocket.getInputStream();
InputStreamReader isr= new InputStreamReader(in);
BufferedReader reader = new BufferedReader(isr);
String content = reader.readLine();
System.out.println(content);
printer.println("USER "+ getUser() + "\r\n" + "PASS" + getPassword() + "\r\n" + "PWD" + "\r\n" +"PASV" + "\r\n"+ "LIST" + "\r\n");
//printer.println("USER "+ getUser() + "\r\n" + "PASS" + getPassword() + "\r\n" + "PWD" + "\r\n" +"PASV" + "\r\n"+ "LIST" + "\r\n");
printer.println("USER "+ getUser().trim()+"\r\n");
content = reader.readLine();
System.out.println(content);
printer.println("PASS "+ getPassword().trim()+"\r\n");
content = reader.readLine();
System.out.println(content);
/*
content = reader.readLine();
System.out.println(content);
......@@ -59,10 +78,24 @@ public class ClientFTP {
String pasv = content.substring(content.indexOf("(")+1,content.indexOf(")"));
String [] mysplit = pasv.split(",");
String newip = mysplit[0]+"."+mysplit[1]+"."+mysplit[2]+"."+mysplit[3];
int newport = Integer.parseInt(mysplit[4])*256+ Integer.parseInt(mysplit[5]);
*/
int newport = 0;
String newip = null;
while((content = reader.readLine()) !=null) {
if (content.toLowerCase().startsWith("227 entering passive mode")) {
String temp = content.substring(content.indexOf("(")+1,content.indexOf(")"));
ip = getIP(temp);
newport = getPort(temp);
getData(newip, newport,printer,reader);
}
}
this.mysocket.close();
/*
Socket newsocket = new Socket(newip,newport);
OutputStream newout = newsocket.getOutputStream();
......@@ -77,6 +110,21 @@ public class ClientFTP {
content = reader.readLine();
System.out.println(content);
*/
}
private void getData(String ip,int newport,PrintWriter myprinter,BufferedReader myreader) throws UnknownHostException, IOException {
Socket mySocket;
mySocket = new Socket(ip,newport);
InputStream in = mySocket.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader myreader2 = new BufferedReader(isr);
String dir;
while((dir = myreader2.readLine())!=null){
System.out.println(dir);
}
mySocket.close();
}
}
/sr1/
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment