Advertisement
Guest User

ddd

a guest
Sep 22nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import java.io.DataOutputStream;
  2. import java.io.DataInputStream;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.PrintWriter;
  6. import java.io.IOException;
  7. import java.net.InetAddress;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10. import java.net.ServerSocket;
  11. import java.util.concurrent.Semaphore;
  12.  
  13. class TCPClient implements Runnable{
  14.  
  15. private Socket connection;
  16. private String filePath;
  17.  
  18. public TCPClient(String filePath) {
  19. super();
  20. try {
  21. connection = new Socket(InetAddress.getByName("localhost"), 9876);
  22. } catch (UnknownHostException e) {
  23. e.printStackTrace();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27.  
  28. this.filePath = filePath;
  29.  
  30. }
  31.  
  32. public long totalSizeRucursive(File folder) {
  33.  
  34. int tot = 0;
  35. File[] files = folder.listFiles();
  36.  
  37. for(File f : files) {
  38.  
  39. if(f.isFile() && f.getName().endsWith(".jpg")) {
  40.  
  41. tot += f.length();
  42.  
  43. }
  44. else if(f.isDirectory()) {
  45. tot += totalSizeRucursive(f);
  46. }
  47.  
  48. }
  49.  
  50. return tot;
  51.  
  52. }
  53.  
  54. @Override
  55. public void run() {
  56.  
  57. long total = totalSizeRucursive(new File(filePath));
  58.  
  59. try {
  60. DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
  61. dos.writeLong(total);
  62. dos.flush();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. }
  68.  
  69. }
  70.  
  71. class TCPServer {
  72.  
  73. private ServerSocket server;
  74. private File destination;
  75.  
  76. public TCPServer(int port) {
  77.  
  78. try {
  79. server = new ServerSocket(port);
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83.  
  84. destination = new File("filesizes.txt");
  85.  
  86. if(!destination.exists()) {
  87. try {
  88. destination.createNewFile();
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. }
  94.  
  95. public void listen() {
  96.  
  97. while(true) {
  98.  
  99. Socket client = null;
  100. try {
  101. client = server.accept();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. }
  105.  
  106. Thread t = new Thread(new TCPThread(client, destination));
  107. t.start();
  108.  
  109. }
  110.  
  111. }
  112. }
  113.  
  114. class TCPThread implements Runnable {
  115.  
  116. private Socket client;
  117. private DataInputStream dis;
  118. private PrintWriter pw;
  119. private static Semaphore sem = new Semaphore(1);
  120.  
  121. public TCPThread(Socket client, File file) {
  122. super();
  123. this.client = client;
  124. try {
  125. dis = new DataInputStream(client.getInputStream());
  126. pw = new PrintWriter(new FileWriter(file, true));
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131.  
  132. @Override
  133. public void run() {
  134. try {
  135. long total = dis.readLong();
  136. String address = client.getInetAddress().getHostAddress();
  137. int port = client.getPort();
  138.  
  139. try {
  140. sem.acquire();
  141. } catch (InterruptedException e) {
  142. e.printStackTrace();
  143. }
  144. pw.println(address + " " + port + " " + total);
  145. sem.release();
  146.  
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement