|
| 1 | +import java.io.ObjectOutputStream; |
| 2 | +import java.net.Socket; |
| 3 | + |
| 4 | + |
| 5 | +public class Client { |
| 6 | + private String dirName; |
| 7 | + private String serverIP; |
| 8 | + private int PORT_NUMBER; |
| 9 | + |
| 10 | + public Client(String dirName, String serverIP, int port) { |
| 11 | + this.dirName = dirName; |
| 12 | + this.serverIP = serverIP; |
| 13 | + PORT_NUMBER = port; |
| 14 | + |
| 15 | + System.out.println("Client Selected!"); |
| 16 | + System.out.println("Dir to sync: " + dirName); |
| 17 | + System.out.println("Server IP: " + serverIP); |
| 18 | + } |
| 19 | + |
| 20 | + public void runClient() throws Exception { |
| 21 | + String localDirName = dirName; //cleaning up the users input |
| 22 | + if(dirName.contains("/")){ |
| 23 | + if(dirName.lastIndexOf("/") != (dirName.length() - 1)) { |
| 24 | + localDirName = dirName.substring(dirName.lastIndexOf("/")); |
| 25 | + } else { |
| 26 | + localDirName = dirName.substring(0, (dirName.length() - 1)); |
| 27 | + if(localDirName.contains("/")) |
| 28 | + localDirName = localDirName.substring(localDirName.lastIndexOf("/")); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if(localDirName.equals(".")){ |
| 33 | + System.out.println("Please input a dir name instead of ./ or ."); |
| 34 | + Thread.sleep(10); |
| 35 | + System.exit(0); |
| 36 | + } |
| 37 | + |
| 38 | + if(!localDirName.startsWith("./")){ //still cleaning up their input |
| 39 | + if(localDirName.startsWith("/")) |
| 40 | + localDirName = "." + localDirName; |
| 41 | + else |
| 42 | + localDirName = "./" + localDirName; |
| 43 | + } |
| 44 | + |
| 45 | + Socket sock = new Socket(serverIP, PORT_NUMBER); |
| 46 | + ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); |
| 47 | + oos.writeObject(localDirName); |
| 48 | + oos.flush(); |
| 49 | + |
| 50 | +/* //check to see if file exists on server |
| 51 | + ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); |
| 52 | + String fExists = (String) ois.readObject(); |
| 53 | +
|
| 54 | + if(fExists.equals("1")){ |
| 55 | + System.out.print("Receiving file: "); |
| 56 | + byte[] mybytearray = new byte[1024]; |
| 57 | + InputStream is = sock.getInputStream(); |
| 58 | + FileOutputStream fos = new FileOutputStream(fileName); |
| 59 | + BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 60 | + int bytesRead = 0; |
| 61 | + int current = 0; |
| 62 | + while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){ |
| 63 | + bos.write(mybytearray, 0, bytesRead); |
| 64 | + current = current + bytesRead; |
| 65 | + System.out.print("."); |
| 66 | + } |
| 67 | + System.out.println(); |
| 68 | + System.out.println("Done!"); |
| 69 | + oos.close(); |
| 70 | + ois.close(); |
| 71 | + bos.close(); |
| 72 | + }else{ |
| 73 | + System.out.println("Server replied that " + fileName + " does not exist, closing connection."); |
| 74 | + } |
| 75 | +*/ oos.close(); |
| 76 | + sock.close(); |
| 77 | + } |
| 78 | +} |
0 commit comments