Advertisement
Guest User

Untitled

a guest
May 26th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. package Client;
  2.  
  3. import java.io.*;
  4. import java.net.InetAddress;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. import Server.FileUtils;
  9.  
  10.  
  11. public class Client_main {
  12.  
  13. public static void main(String [] args)
  14. {
  15. run();
  16. }
  17.  
  18.  
  19. public static void run()
  20. {
  21. try
  22. {
  23.  
  24. InetAddress address = InetAddress.getByAddress(new byte [] {127,0,0,1});
  25. Socket socket = new Socket(address, 5505);
  26. //////////////////////////
  27. InputStream is = socket.getInputStream();
  28. OutputStream os = socket.getOutputStream();
  29. DataOutputStream dos = new DataOutputStream(os);
  30. DataInputStream dis = new DataInputStream (is);
  31. ////////////////////////////
  32. Scanner sc = new Scanner (System.in);
  33. String command = null;
  34. String answer = null;
  35. String put_server = "server";
  36. String put_client = "client";
  37. try
  38. {
  39. while (true)
  40. {
  41. System.out.print(">>>");
  42. command = sc.nextLine();
  43. String[] params = command.split("\\s+");
  44. if ((params[0].equalsIgnoreCase("quit")) || (params[0].equalsIgnoreCase("exit"))|| (params[0].equalsIgnoreCase("q")) )
  45. {
  46. //socket.close();
  47. socket.close();
  48. return;
  49. }
  50.  
  51. else
  52. if ((params[0].equalsIgnoreCase("pwd")) || (params[0].equalsIgnoreCase("cd")) || (params[0].equalsIgnoreCase("ls")))
  53. {
  54. dos.writeUTF(command);
  55. answer = dis.readUTF();
  56. System.out.print(answer);
  57. if (params[0].equalsIgnoreCase("cd"))
  58. {
  59. put_server = answer;
  60. }
  61. }
  62. //send get(name)
  63. // answer: ok<size>
  64. // ANSWER: error <message>
  65.  
  66. else if (params[0].equalsIgnoreCase("put"))
  67. {
  68. dos.writeUTF(command);
  69. String[] file_ = command.split(" ");
  70. String filename = file_[1];
  71.  
  72. FileInputStream fin = new FileInputStream(new File(put_client, filename));
  73. Socket socket2 = new Socket(address, 7711);
  74. OutputStream os2 = socket2.getOutputStream();
  75. DataOutputStream dos2 = new DataOutputStream(os2);
  76. FileUtils.copyStream(fin, dos2);
  77.  
  78. socket2.close();
  79. answer = dis.readUTF();
  80. System.out.print(answer);
  81. }
  82.  
  83. else
  84. if (params[0].equalsIgnoreCase("get"))
  85. {
  86. dos.writeUTF(command);
  87. String[] file_ = command.split(" ");
  88. String filename = file_[1];
  89. String file_output =filename;
  90. ////////////////////////
  91. int i = 1;
  92. while (new File(put_client, file_output).exists() == true)
  93. {
  94. file_output = "("+i+")"+filename;
  95. i = i+1;
  96. }
  97.  
  98. File file_out = new File(put_client, file_output);
  99. OutputStream os_text = new FileOutputStream(file_out);
  100.  
  101. Socket socket3 = new Socket(address, 7710);
  102. OutputStream os3 = socket3.getOutputStream();
  103. DataOutputStream dos3 = new DataOutputStream(os3);
  104. InputStream is3 = socket3.getInputStream();
  105. DataInputStream dis3 = new DataInputStream (is3);
  106.  
  107. FileUtils.copyStream(is3, os_text);
  108. socket3.close();
  109. answer = dis.readUTF();
  110. System.out.print(answer);
  111.  
  112. }
  113.  
  114. else
  115. if (params[0].equalsIgnoreCase("lpwd"))
  116. {
  117. System.out.print(put_client);
  118. }
  119.  
  120.  
  121. else
  122. if (params[0].equalsIgnoreCase("lls"))
  123. {
  124. String dirname = put_client; // имя каталога
  125. File fl = new File(dirname);
  126. if (fl.isDirectory())
  127. {
  128. // является ли fl каталогом
  129. //// System.out.println("Directory of " + dirname);
  130. String s[]=fl.list();
  131. for (int i=0; i < s.length; i++)
  132. {
  133. File f = new File(dirname + "/" + s[i]);
  134. if(f.isDirectory())
  135. {
  136. // является ли f каталогом
  137. System.out.println(s[i] + " - directory");
  138. }
  139. else
  140. {
  141. System.out.println(s[i] + " - file");
  142. }
  143. }
  144. }
  145. else
  146. {
  147. System.out.println(dirname + " is not a directory");
  148. }
  149. }
  150.  
  151. else
  152. if (params[0].equalsIgnoreCase("lcd"))
  153. {
  154. String[] file_ = command.split(" ");
  155. String filename = "client";
  156. try{
  157. filename = file_[1];
  158. }
  159. catch (Exception ex)
  160. {
  161. filename = "client";
  162. }
  163.  
  164. if (filename == "")
  165. {
  166. put_client = "client";
  167. }
  168. put_client = filename;
  169. }
  170.  
  171. else
  172. if (params[0].equalsIgnoreCase("help"))
  173. {
  174. System.out.println("pwd - печатает путь к текущему каталогу на сервере") ;
  175. System.out.println("lpwd - печатает путь к текущему каталогу на клиенте") ;
  176. System.out.println("ls - отображает содержимое текущего каталога на сервере") ;
  177. System.out.println("lls - отображает содержимое текущего каталога на клиенте") ;
  178. System.out.println("cd - меняет текущий каталог на сервере") ;
  179. System.out.println("lcd - меняет текущий каталог на клиенте") ;
  180. System.out.println("put <имя файла> - передает файл на сервер") ;
  181. System.out.println("get <имя файла> - передает файл с сервера на клиент") ;
  182. System.out.println("quit - завершает соединение") ;
  183. System.out.println("help - печатает справку по командам") ;
  184. }
  185.  
  186. else
  187. {
  188. System.out.println("No such command");
  189. }
  190.  
  191. }
  192. }
  193. finally
  194. {
  195. socket.close();
  196.  
  197.  
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. ex.printStackTrace();
  203. }
  204. }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement