Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.util.*;
- import java.nio.*;
- import java.nio.channels.FileChannel;
- class ClientThreads extends Thread
- {
- public String path = "";
- public String name1 = "";
- private String clientName = null;
- private DataInputStream is = null;
- private PrintStream os = null;
- private Socket clientSocket = null;
- private final ClientThreads[] threads;
- private int maxClientsCount;
- public int position = 0;
- public static List<String> listName = Collections.synchronizedList(new ArrayList<String>());
- List<String> l = Collections.synchronizedList(new ArrayList<String>());
- public String[] namesList = new String[10];
- public ClientThreads(Socket clientSocket, ClientThreads[] threads,
- String name, String[] namesList, List<String> listName)
- {
- this.clientSocket = clientSocket;
- this.threads = threads;
- maxClientsCount = threads.length;
- this.name1 = name;
- this.namesList = namesList;
- }
- @SuppressWarnings("deprecation")
- public void run()
- {
- int maxClientsCount = this.maxClientsCount;
- ClientThreads[] threads = this.threads;
- synchronized (listName)
- {
- //Iterator i = listName.iterator(); // Must be in synchronized block
- ListIterator<String> i = listName.listIterator();
- }
- try
- {
- is = new DataInputStream(clientSocket.getInputStream());
- os = new PrintStream(clientSocket.getOutputStream());
- String name;
- while (true)
- {
- os.println("What is your name?");
- name = is.readLine().trim();
- break;
- }
- synchronized(listName)
- {
- if(!listName.contains(name))
- {
- if(!listName.contains(name) && name != null && !name.isEmpty())
- {
- listName.add(name);
- Path currentRelativePath = Paths.get("");
- path = currentRelativePath.toAbsolutePath().toString();
- System.out.println("Current relative path is: " + path);
- listName.add(path);
- os.println("The list now contains: " + listName);
- }
- }
- }
- synchronized (this)
- {
- for (int i = 0; i < maxClientsCount; i++)
- {
- if (threads[i] != null && threads[i] == this)
- {
- clientName = "@" + name;
- break;
- }
- }
- for (int i = 0; i < maxClientsCount; i++)
- {
- if (threads[i] != null)
- {
- threads[i].os.println(name + " has connected.");
- os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
- }
- }
- }
- while (true)
- {
- synchronized(listName)
- {
- //if(!listName.contains(name))
- //{
- // listName.add(name);
- // //os.println("The name " + name + " has been added to the list. "
- // + "The list now contains 12314151" + listName);
- //}
- }
- String line = is.readLine();
- if (line.startsWith("/quit"))
- {
- break;
- }
- else
- {
- //os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
- }
- if(line.equals("x") || line.equals("X"))
- {
- os.println("x");
- os.println(name + "has disconnected.");
- listName.remove(name);
- listName.remove(this);
- listName.remove(path);
- os.println("The list now contains: " + listName);
- System.out.println("A user disconnected. The list now contains: " +listName);
- }
- if(line.equals("m") || line.equals("M"))
- {
- os.println("Enter your message: ");
- }
- if(line.equals("f") || line.equals("F"))
- {
- os.println("Who owns the file?");
- boolean keep = true;
- while (keep == true)
- {
- String fileOwner = is.readLine();
- if(fileOwner !=null && !fileOwner.isEmpty())
- {
- os.println(fileOwner);
- System.out.println(fileOwner);
- // path = path + fileOwner;
- File folder = new File(path);
- File[] listOfFiles = folder.listFiles();
- os.println("Files to choose from: ");
- for (int i = 0; i < listOfFiles.length; i++)
- {
- if (listOfFiles[i].isFile())
- {
- os.println(listOfFiles[i].getName());
- System.out.println(listOfFiles[i].getName());
- }
- else if (listOfFiles[i].isDirectory())
- {
- System.out.println("Folder: " + listOfFiles[i].getName());
- os.println("Folder: " + listOfFiles[i].getName());
- }
- }
- keep = false;
- os.println("Which file do you want?");
- boolean keep2 = true;
- while(keep2==true)
- {
- String filename = is.readLine();
- if(filename !=null && !filename.isEmpty())
- {
- File src = new File(filename);
- String recieved = "recievedFile" +filename;
- File target = new File(recieved);
- Files.copy(src.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
- this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
- keep2=false;
- }
- }
- }
- }
- keep=false;
- }
- else
- {
- synchronized (this)
- {
- for (int i = 0; i < maxClientsCount; i++)
- {
- 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"))
- {
- threads[i].os.println(name + ": " + line + "\n");
- this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
- }
- }
- }
- }
- }
- synchronized (this)
- {
- for (int i = 0; i < maxClientsCount; i++)
- {
- if (threads[i] != null && threads[i] != this && threads[i].clientName != null)
- {
- threads[i].os.println(name + "has disconnected.");
- threads[i].listName.remove(name);
- listName.remove(name);
- threads[i].os.println("The list now contains: " + listName);
- System.out.println("A user disconnected. The list now contains: " +listName);
- }
- }
- }
- synchronized (this)
- {
- for (int i = 0; i < maxClientsCount; i++)
- {
- if (threads[i] == this)
- {
- threads[i] = null;
- }
- }
- }
- is.close();
- os.close();
- clientSocket.close();
- } catch (IOException e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment