Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Client;
- import java.io.*;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.util.Scanner;
- import Server.FileUtils;
- public class Client_main {
- public static void main(String [] args)
- {
- run();
- }
- public static void run()
- {
- try
- {
- InetAddress address = InetAddress.getByAddress(new byte [] {127,0,0,1});
- Socket socket = new Socket(address, 5505);
- //////////////////////////
- InputStream is = socket.getInputStream();
- OutputStream os = socket.getOutputStream();
- DataOutputStream dos = new DataOutputStream(os);
- DataInputStream dis = new DataInputStream (is);
- ////////////////////////////
- Scanner sc = new Scanner (System.in);
- String command = null;
- String answer = null;
- String put_server = "server";
- String put_client = "client";
- try
- {
- while (true)
- {
- System.out.print(">>>");
- command = sc.nextLine();
- String[] params = command.split("\\s+");
- if ((params[0].equalsIgnoreCase("quit")) || (params[0].equalsIgnoreCase("exit"))|| (params[0].equalsIgnoreCase("q")) )
- {
- //socket.close();
- socket.close();
- return;
- }
- else
- if ((params[0].equalsIgnoreCase("pwd")) || (params[0].equalsIgnoreCase("cd")) || (params[0].equalsIgnoreCase("ls")))
- {
- dos.writeUTF(command);
- answer = dis.readUTF();
- System.out.print(answer);
- if (params[0].equalsIgnoreCase("cd"))
- {
- put_server = answer;
- }
- }
- //send get(name)
- // answer: ok<size>
- // ANSWER: error <message>
- else if (params[0].equalsIgnoreCase("put"))
- {
- dos.writeUTF(command);
- String[] file_ = command.split(" ");
- String filename = file_[1];
- FileInputStream fin = new FileInputStream(new File(put_client, filename));
- Socket socket2 = new Socket(address, 7711);
- OutputStream os2 = socket2.getOutputStream();
- DataOutputStream dos2 = new DataOutputStream(os2);
- FileUtils.copyStream(fin, dos2);
- socket2.close();
- answer = dis.readUTF();
- System.out.print(answer);
- }
- else
- if (params[0].equalsIgnoreCase("get"))
- {
- dos.writeUTF(command);
- String[] file_ = command.split(" ");
- String filename = file_[1];
- String file_output =filename;
- ////////////////////////
- int i = 1;
- while (new File(put_client, file_output).exists() == true)
- {
- file_output = "("+i+")"+filename;
- i = i+1;
- }
- File file_out = new File(put_client, file_output);
- OutputStream os_text = new FileOutputStream(file_out);
- Socket socket3 = new Socket(address, 7710);
- OutputStream os3 = socket3.getOutputStream();
- DataOutputStream dos3 = new DataOutputStream(os3);
- InputStream is3 = socket3.getInputStream();
- DataInputStream dis3 = new DataInputStream (is3);
- FileUtils.copyStream(is3, os_text);
- socket3.close();
- answer = dis.readUTF();
- System.out.print(answer);
- }
- else
- if (params[0].equalsIgnoreCase("lpwd"))
- {
- System.out.print(put_client);
- }
- else
- if (params[0].equalsIgnoreCase("lls"))
- {
- String dirname = put_client; // имя каталога
- File fl = new File(dirname);
- if (fl.isDirectory())
- {
- // является ли fl каталогом
- //// System.out.println("Directory of " + dirname);
- String s[]=fl.list();
- for (int i=0; i < s.length; i++)
- {
- File f = new File(dirname + "/" + s[i]);
- if(f.isDirectory())
- {
- // является ли f каталогом
- System.out.println(s[i] + " - directory");
- }
- else
- {
- System.out.println(s[i] + " - file");
- }
- }
- }
- else
- {
- System.out.println(dirname + " is not a directory");
- }
- }
- else
- if (params[0].equalsIgnoreCase("lcd"))
- {
- String[] file_ = command.split(" ");
- String filename = "client";
- try{
- filename = file_[1];
- }
- catch (Exception ex)
- {
- filename = "client";
- }
- if (filename == "")
- {
- put_client = "client";
- }
- put_client = filename;
- }
- else
- if (params[0].equalsIgnoreCase("help"))
- {
- System.out.println("pwd - печатает путь к текущему каталогу на сервере") ;
- System.out.println("lpwd - печатает путь к текущему каталогу на клиенте") ;
- System.out.println("ls - отображает содержимое текущего каталога на сервере") ;
- System.out.println("lls - отображает содержимое текущего каталога на клиенте") ;
- System.out.println("cd - меняет текущий каталог на сервере") ;
- System.out.println("lcd - меняет текущий каталог на клиенте") ;
- System.out.println("put <имя файла> - передает файл на сервер") ;
- System.out.println("get <имя файла> - передает файл с сервера на клиент") ;
- System.out.println("quit - завершает соединение") ;
- System.out.println("help - печатает справку по командам") ;
- }
- else
- {
- System.out.println("No such command");
- }
- }
- }
- finally
- {
- socket.close();
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement