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