Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.Socket;
  3. import java.util.StringTokenizer;
  4.  
  5. public class Client
  6. {
  7.     public static Boolean PWD(PrintWriter writer, BufferedReader br) throws IOException
  8.     {
  9.         String str = new String();
  10.         writer.println("PWD");  
  11.         str = br.readLine();
  12.         if(str.substring(0,3).equals("257")){
  13.             System.out.println("PWD : "+str.substring(3));
  14.             return true;
  15.         }
  16.         return false;
  17.     }
  18.    
  19.     public static Boolean CWD(PrintWriter writer, BufferedReader br) throws IOException
  20.     {
  21.         String str = new String();
  22.         writer.println("CWD "+"WinXp");  
  23.         str = br.readLine();
  24.         //System.out.println(str);
  25.         if(str.substring(0,3).equals("250")){
  26.             System.out.println(str.substring(3));
  27.             return true;
  28.         }
  29.         return false;
  30.     }
  31.    
  32.     public static Boolean PASV(PrintWriter writer, BufferedReader br) throws IOException
  33.     {
  34.         String str = new String();
  35.         int j=0;
  36.         String tabString[]=new String[5];
  37.         writer.println("PASV"+str);  
  38.         str = br.readLine();
  39.         StringTokenizer ip = new StringTokenizer(str, "(), ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopkrstuvwxyz");
  40.         //ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopkrstuvwxyz
  41.  
  42.         for(int i=0;i<3;i++){
  43.             ip.nextToken();
  44.         }
  45.         while(ip.hasMoreTokens())
  46.         {
  47.             tabString[j]=ip.nextToken();
  48.             System.out.print(tabString[j]+".");
  49.             j++;
  50.         }
  51.         if(str.substring(0,3).equals("227")){
  52.             System.out.println(str.substring(3));
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.    
  58.     public static Boolean QUIT(PrintWriter writer, BufferedReader br) throws IOException
  59.     {
  60.         String str = new String();
  61.         writer.println("QUIT");  
  62.         str = br.readLine();
  63.         if(str.substring(0,3).equals("221")){
  64.             System.out.println(str.substring(3));
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69.    
  70.    
  71.     public static Boolean connexion(PrintWriter writer, BufferedReader br) throws IOException
  72.     {
  73.         BufferedReader entree = new BufferedReader(new InputStreamReader(System.in));
  74.         String str = new String();
  75.         //String user = "e20703933";
  76.         //String pass = "pemeenam1";
  77.         String response = new String();
  78.         response = br.readLine();
  79.         System.out.println(response.substring(0, 3));
  80.         if(response.substring(0,3).equals("220"))
  81.         {
  82.             System.out.print("Username : ");
  83.             str = entree.readLine();
  84.             writer.println("USER "+str);
  85.             response = br.readLine();
  86.             if(response.substring(0,3).equals("331"))
  87.             {
  88.                 System.out.print("Password : ");
  89.                 str = entree.readLine();
  90.                 writer.println("PASS "+str);
  91.                 response = br.readLine();
  92.                 //System.out.print(response);
  93.                 if(response.substring(0,3).equals("230"))
  94.                 {  
  95.                     System.out.println("Connection successfull !");
  96.                     return true;
  97.                 }
  98.                 System.out.println("Wrong password");
  99.                 return false;
  100.                
  101.             }
  102.             return false;
  103.         }
  104.         System.out.println(response);
  105.         return false;
  106.     }
  107.  
  108.     public static void main(String [] args) {
  109.         System.getProperties().put("http.proxyHost", "proxyubo.univ-brest.fr");
  110.         System.getProperties().put("http.proxyPort", "3128");
  111.        
  112.         try{
  113.            
  114.             String addr = "stockage.univ-brest.fr";
  115.             Socket sock = new Socket(addr,21);
  116.            
  117.             PrintWriter writer = new PrintWriter(
  118.                       new BufferedWriter(
  119.                          new OutputStreamWriter(sock.getOutputStream())),
  120.                       true);
  121.  
  122.             BufferedReader br = new BufferedReader(
  123.                     new InputStreamReader(sock.getInputStream())
  124.                     );
  125.            
  126.             if(connexion(writer,br)){
  127.                 if(PWD(writer,br))
  128.                 {
  129.                     if(CWD(writer,br))
  130.                     {
  131.                         if(PWD(writer,br))
  132.                         {
  133.                             if(PASV(writer,br)){
  134.                                 QUIT(writer,br);   
  135.                             }
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.            
  141.            
  142.             writer.println("END") ;
  143.             br.close();
  144.             writer.close();
  145.             sock.close();
  146.            
  147.         }catch (IOException ioe){}
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement