////////////////////////////////////////////////////////////////////////////////// //Client class. ////////////////////////////////////////////////////////////////////////////////// import java.net.*; import java.io.*; class Client { static DataInputStream recvBuf = null; //Leitura. static DataOutputStream sendBuf = null; //Escrita. static Socket tSocket = null; // Data connection. static DataInputStream userInput = new DataInputStream( new BufferedInputStream(System.in)); static String buffer = null; //comando inteiro static String command = null; //comando static String param = null; //parametro static int localPort; static String listDir[] = new String[1000]; static int listDirLen = 0; public static void main(String argv[]) throws IOException { if (argv.length != 2) System.out.println("Número de parametros incorreto !!"); else { // Envia mensagem na connection. try { // Cria o socket. tSocket = new Socket(argv[0], Integer.parseInt(argv[1])); localPort = tSocket.getLocalPort(); // Output stream. sendBuf = new DataOutputStream( new BufferedOutputStream(tSocket.getOutputStream())); // Input stream recvBuf = new DataInputStream( new BufferedInputStream(tSocket.getInputStream())); } catch (UnknownHostException e) { System.out.println("Don't know about host."); } catch (IOException e) { System.out.println(e); } buffer = recvBuf.readLine(); System.out.println(buffer + "\r\n"); //Imprime mensagem do servidor while(true) { System.out.print("ftp>"); buffer = userInput.readLine(); int index = buffer.indexOf(" "); if (index != -1) { command = buffer.substring(0, index).toLowerCase(); param = buffer.substring((index+1), buffer.length()); } else { param = null; command = buffer.toLowerCase(); } if (command.equals("user")) { user(param); } else if (command.equals("pass")) { pass(param); } else if (command.equals("ls")) { nlst(param); } else if (command.equals("pwd")) { pwd(param); } else if (command.equals("cd")) { cwd(param); } else if (command.equals("lls")) { lls(param); } else if (command.equals("lcd")) { lcd(param); } else if (command.equals("get")) { get(param); } else if (command.equals("put")) { put(param); } else if (command.equals("mget")) { mget(param); } else if (command.equals("mput")) { mput(param); } else if (command.equals("quit")) { quit(param); } } //while } } //============================================================================== // Comandos. //============================================================================== //------------------------------------------------------------------------------ // USER. // static void user(String param) { if(param == null) { System.out.println("Falta o parâmetro"); return; } else { try { sendBuf.writeBytes("USER "+param+"\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } } //------------------------------------------------------------------------------ // PASS. // static void pass(String param) { if(param == null) { System.out.println("Falta o parâmetro"); return; } else { try { sendBuf.writeBytes("PASS "+param+"\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } } //------------------------------------------------------------------------------ // NLST. // static void nlst(String param) { String newPort; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(2222,1); // newPort = port(serverSocket.getInetAddress(),serverSocket.getLocalPort()); // System.out.println(newPort); } catch (IOException e) { System.out.println("Could not listen on port. " + e); return; } //------------------------------------------------------------------------------ // Envia o comando. if(param == null) { try { sendBuf.writeBytes("PORT 143,107,231,208,22,22\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); sendBuf.writeBytes("NLST\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } else { try { sendBuf.writeBytes("PORT 143,107,231,208,22,22\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); sendBuf.writeBytes("NLST "+param+"\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } //------------------------------------------------------------------------------- // Espera pela conexão. // Listen na porta que estou conectado. Socket newSocket = null; try { newSocket = serverSocket.accept(); serverSocket.close(); } catch (IOException e) { System.out.println("Accept failed: "+ e); return; } //------------------------------------------------------------------------------- // Recebe o ls. DataInputStream recvDataBuf; try { recvDataBuf = new DataInputStream( new BufferedInputStream(newSocket.getInputStream())); //--------------------------------------------------------------- // Recebe o arquivo. String uga = null; uga = recvDataBuf.readLine(); listDirLen = 0; while(uga != null) { listDir[listDirLen] = new String(uga); listDirLen++; System.out.println(uga); uga = recvDataBuf.readLine(); } System.out.println(recvBuf.readLine()); try { serverSocket.close(); newSocket.close(); } catch (IOException e2) { System.out.println(e2); } } catch (IOException e) { System.out.println(e); return; } } //------------------------------------------------------------------------------ // PWD. // static void pwd(String param) { try { sendBuf.writeBytes("PWD\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } //------------------------------------------------------------------------------ // CWD. // static void cwd(String param) { if(param == null) { System.out.println("Falta o parâmetro"); return; } else { try { sendBuf.writeBytes("CWD "+param+"\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } } //------------------------------------------------------------------------------ // LLS. // static void lls(String param) { File listFile = new File(System.getProperty("user.dir")); String listDir[] = listFile.list(); for(int i = 0; i < listDir.length; i++) { System.out.println(listDir[i]); } } //------------------------------------------------------------------------------ // LCD. // static void lcd(String param) { if(param == null) { System.out.println("Falta parâmetro"); return; } try { File listFile = new File(System.getProperty("user.dir"),param); if(listFile.exists()) { java.util.Properties sysProp = System.getProperties(); sysProp.put("user.dir",listFile.getCanonicalPath()); System.setProperties(sysProp); System.out.println("lcd ok: "+System.getProperty("user.dir")); } else { System.out.println("Diretório inválido."); } } catch (IOException e) { System.out.println(e); } } //------------------------------------------------------------------------------ // GET. // static void get(String param) { String newPort; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(2222,1); // newPort = port(serverSocket.getInetAddress(),serverSocket.getLocalPort()); // System.out.println(newPort); } catch (IOException e) { System.out.println("Could not listen on port. " + e); return; } //------------------------------------------------------------------------------ // Envia o comando. if(param == null) { System.out.println("Falta o parâmetro."); } else { try { sendBuf.writeBytes("PORT 143,107,231,208,22,22\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); sendBuf.writeBytes("RETR "+param+"\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } //------------------------------------------------------------------------------- // Espera pela conexão. // Listen na porta que estou conectado. Socket newSocket = null; try { newSocket = serverSocket.accept(); serverSocket.close(); } catch (IOException e) { System.out.println("Accept failed: "+ e); return; } DataInputStream recvDataBuf; FileOutputStream writeFile = null; try { recvDataBuf = new DataInputStream(new BufferedInputStream(newSocket.getInputStream())); writeFile = new FileOutputStream(System.getProperty("user.dir")+"/"+param); int byteValue; //--------------------------------------------------------------- // Envia o arquivo. while(true) { byteValue = recvDataBuf.readByte(); writeFile.write(byteValue); } } catch (EOFException e) { try { //--------------------------------------------------------------- // Termina a conexão de dado. writeFile.close(); newSocket.close(); System.out.println(recvBuf.readLine()); } catch (IOException e2) { System.out.println(e2); } } catch (IOException e) { System.out.println(e); } } //------------------------------------------------------------------------------ // PUT. // static void put(String param) { String newPort; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(2222,1); // newPort = port(serverSocket.getInetAddress(),serverSocket.getLocalPort()); // System.out.println(newPort); } catch (IOException e) { System.out.println("Could not listen on port. " + e); return; } //------------------------------------------------------------------------------ // Envia o comando. if(param == null) { System.out.println("Falta o parâmetro."); } else { try { sendBuf.writeBytes("PORT 143,107,231,208,22,22\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); sendBuf.writeBytes("STOR "+param+"\r\n"); sendBuf.flush(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } //------------------------------------------------------------------------------- // Espera pela conexão. // Listen na porta que estou conectado. Socket newSocket = null; try { newSocket = serverSocket.accept(); serverSocket.close(); } catch (IOException e) { System.out.println("Accept failed: "+ e); return; } DataOutputStream sendDataBuf = null; File listFile = new File(System.getProperty("user.dir"),param); //----------------------------------------------------------------------------------- // Envia o arquivo se existir. // if(listFile.exists()) { if(listFile.isFile() && listFile.canRead()) { //-------------------------------------------------------------------- // Envia mensagem na data connection. try { // Output stream. sendDataBuf = new DataOutputStream(new BufferedOutputStream(newSocket.getOutputStream())); } catch (IOException e) { System.out.println(e); } try { FileInputStream readFile = new FileInputStream(listFile); int byteValue; //--------------------------------------------------------------- // Envia o arquivo. while((byteValue = readFile.read()) != -1) { sendDataBuf.writeByte(byteValue); } sendDataBuf.flush(); //--------------------------------------------------------------- // Termina a conexão de dado. newSocket.close(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } else if(listFile.isDirectory()) { try { System.out.println("Arquivo inválido."); newSocket.close(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } } else { try { System.out.println("Arquivo inválido."); newSocket.close(); System.out.println(recvBuf.readLine()); } catch (IOException e) { System.out.println(e); } } } //------------------------------------------------------------------------------ // MGET. // static void mget(String param) { if(param == null) { System.out.println("Falta o parâmetro"); return; } else { nlst(param); if(listDirLen==0) { System.out.println("Nada para trazer."); return; } for(int i=0;i> 8 & 0xff); pa[4] = toUnsignedDecimal(bhi); byte blo = (byte)(port & 0xff); pa[5] = toUnsignedDecimal(blo); // Now concatenate these pieces, and return the value String s = pa[0] + "," + pa[1] + "," + pa[2] + "," + pa[3] + "," + pa[4] + "," + pa[5]; return s; } */ }