wieruowq

ClientThreads.java

Jun 8th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.nio.file.StandardCopyOption;
  7. import java.util.*;
  8. import java.nio.*;
  9. import java.nio.channels.FileChannel;
  10. class ClientThreads extends Thread
  11. {
  12. public String path = "";
  13. public String name1 = "";
  14. private String clientName = null;
  15. private DataInputStream is = null;
  16. private PrintStream os = null;
  17. private Socket clientSocket = null;
  18. private final ClientThreads[] threads;
  19. private int maxClientsCount;
  20. public int position = 0;
  21. public static List<String> listName = Collections.synchronizedList(new ArrayList<String>());
  22. List<String> l = Collections.synchronizedList(new ArrayList<String>());
  23. public String[] namesList = new String[10];
  24. public ClientThreads(Socket clientSocket, ClientThreads[] threads,
  25. String name, String[] namesList, List<String> listName)
  26. {
  27. this.clientSocket = clientSocket;
  28. this.threads = threads;
  29. maxClientsCount = threads.length;
  30. this.name1 = name;
  31. this.namesList = namesList;
  32.  
  33. }
  34.  
  35.  
  36.  
  37. @SuppressWarnings("deprecation")
  38. public void run()
  39. {
  40. int maxClientsCount = this.maxClientsCount;
  41. ClientThreads[] threads = this.threads;
  42. synchronized (listName)
  43. {
  44. //Iterator i = listName.iterator(); // Must be in synchronized block
  45.  
  46. ListIterator<String> i = listName.listIterator();
  47. }
  48. try
  49. {
  50. is = new DataInputStream(clientSocket.getInputStream());
  51. os = new PrintStream(clientSocket.getOutputStream());
  52. String name;
  53. while (true)
  54. {
  55. os.println("What is your name?");
  56. name = is.readLine().trim();
  57. break;
  58.  
  59.  
  60.  
  61. }
  62. synchronized(listName)
  63. {
  64. if(!listName.contains(name))
  65. {
  66. if(!listName.contains(name) && name != null && !name.isEmpty())
  67. {
  68. listName.add(name);
  69.  
  70. Path currentRelativePath = Paths.get("");
  71. path = currentRelativePath.toAbsolutePath().toString();
  72. System.out.println("Current relative path is: " + path);
  73. listName.add(path);
  74. os.println("The list now contains: " + listName);
  75. }
  76.  
  77. }
  78.  
  79.  
  80.  
  81. }
  82. synchronized (this)
  83. {
  84. for (int i = 0; i < maxClientsCount; i++)
  85. {
  86. if (threads[i] != null && threads[i] == this)
  87. {
  88. clientName = "@" + name;
  89. break;
  90. }
  91. }
  92. for (int i = 0; i < maxClientsCount; i++)
  93. {
  94. if (threads[i] != null)
  95. {
  96. threads[i].os.println(name + " has connected.");
  97. os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
  98. }
  99. }
  100. }
  101. while (true)
  102. {
  103. synchronized(listName)
  104. {
  105. //if(!listName.contains(name))
  106. //{
  107. // listName.add(name);
  108. // //os.println("The name " + name + " has been added to the list. "
  109. // + "The list now contains 12314151" + listName);
  110. //}
  111.  
  112. }
  113. String line = is.readLine();
  114. if (line.startsWith("/quit"))
  115. {
  116. break;
  117. }
  118. else
  119. {
  120. //os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
  121. }
  122. if(line.equals("x") || line.equals("X"))
  123. {
  124.  
  125. os.println("x");
  126. os.println(name + "has disconnected.");
  127. listName.remove(name);
  128. listName.remove(this);
  129. listName.remove(path);
  130. os.println("The list now contains: " + listName);
  131. System.out.println("A user disconnected. The list now contains: " +listName);
  132. }
  133. if(line.equals("m") || line.equals("M"))
  134. {
  135. os.println("Enter your message: ");
  136. }
  137. if(line.equals("f") || line.equals("F"))
  138. {
  139.  
  140. os.println("Who owns the file?");
  141. boolean keep = true;
  142. while (keep == true)
  143. {
  144. String fileOwner = is.readLine();
  145.  
  146. if(fileOwner !=null && !fileOwner.isEmpty())
  147. {
  148. os.println(fileOwner);
  149. System.out.println(fileOwner);
  150. // path = path + fileOwner;
  151. File folder = new File(path);
  152. File[] listOfFiles = folder.listFiles();
  153.  
  154. os.println("Files to choose from: ");
  155. for (int i = 0; i < listOfFiles.length; i++)
  156. {
  157. if (listOfFiles[i].isFile())
  158. {
  159. os.println(listOfFiles[i].getName());
  160. System.out.println(listOfFiles[i].getName());
  161. }
  162. else if (listOfFiles[i].isDirectory())
  163. {
  164. System.out.println("Folder: " + listOfFiles[i].getName());
  165. os.println("Folder: " + listOfFiles[i].getName());
  166. }
  167. }
  168.  
  169.  
  170. keep = false;
  171. os.println("Which file do you want?");
  172. boolean keep2 = true;
  173. while(keep2==true)
  174. {
  175. String filename = is.readLine();
  176. if(filename !=null && !filename.isEmpty())
  177. {
  178. File src = new File(filename);
  179. String recieved = "recievedFile" +filename;
  180. File target = new File(recieved);
  181.  
  182. Files.copy(src.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
  183. this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
  184. keep2=false;
  185. }
  186.  
  187. }
  188.  
  189. }
  190. }
  191. keep=false;
  192.  
  193. }
  194. else
  195. {
  196. synchronized (this)
  197. {
  198. for (int i = 0; i < maxClientsCount; i++)
  199. {
  200. if (!line.equals("x") && !line.equals("X") && !line.equals("f") && !line.equals("F") && !line.equals("m") && !line.equals("M") && threads[i] != null && threads[i].clientName != null && !threads[i].clientName.equals("m") && !threads[i].clientName.equals("M"))
  201. {
  202. threads[i].os.println(name + ": " + line + "\n");
  203. this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
  204. }
  205. }
  206. }
  207. }
  208. }
  209. synchronized (this)
  210. {
  211. for (int i = 0; i < maxClientsCount; i++)
  212. {
  213. if (threads[i] != null && threads[i] != this && threads[i].clientName != null)
  214. {
  215. threads[i].os.println(name + "has disconnected.");
  216. threads[i].listName.remove(name);
  217. listName.remove(name);
  218. threads[i].os.println("The list now contains: " + listName);
  219. System.out.println("A user disconnected. The list now contains: " +listName);
  220. }
  221. }
  222. }
  223. synchronized (this)
  224. {
  225. for (int i = 0; i < maxClientsCount; i++)
  226. {
  227. if (threads[i] == this)
  228. {
  229. threads[i] = null;
  230. }
  231. }
  232. }
  233. is.close();
  234. os.close();
  235. clientSocket.close();
  236. } catch (IOException e)
  237. {
  238. }
  239. }
  240.  
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment