Skip to content

Commit 803e63d

Browse files
author
quaa
committed
fixed merge
2 parents 0db3bcb + 1b3b746 commit 803e63d

File tree

4 files changed

+80
-60
lines changed

4 files changed

+80
-60
lines changed

bin/Server.class

-1.72 KB
Binary file not shown.

bin/javaFileSync.class

-3.7 KB
Binary file not shown.

src/Client.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

src/javaFileSync.java

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,66 +35,8 @@ public static void server() throws Exception {
3535
}
3636

3737
public static void client(String dirName, String serverIP) throws Exception {
38-
System.out.println("Client Selected!");
39-
System.out.println("Dir to sync: " + dirName);
40-
System.out.println("Server IP: " + serverIP);
41-
42-
String localDirName = dirName; //cleaning up the users input
43-
if(dirName.contains("/")){
44-
if(dirName.lastIndexOf("/") != (dirName.length() - 1)) {
45-
localDirName = dirName.substring(dirName.lastIndexOf("/"));
46-
} else {
47-
localDirName = dirName.substring(0, (dirName.length() - 1));
48-
if(localDirName.contains("/"))
49-
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
50-
}
51-
}
52-
53-
if(localDirName.equals(".")){
54-
System.out.println("Please input a dir name instead of ./ or .");
55-
Thread.sleep(10);
56-
System.exit(0);
57-
}
58-
59-
if(!localDirName.startsWith("./")){ //still cleaning up their input
60-
if(localDirName.startsWith("/"))
61-
localDirName = "." + localDirName;
62-
else
63-
localDirName = "./" + localDirName;
64-
}
65-
66-
Socket sock = new Socket(serverIP, PORT_NUMBER);
67-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
68-
oos.writeObject(localDirName);
69-
oos.flush();
70-
71-
/* //check to see if file exists on server
72-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
73-
String fExists = (String) ois.readObject();
74-
75-
if(fExists.equals("1")){
76-
System.out.print("Receiving file: ");
77-
byte[] mybytearray = new byte[1024];
78-
InputStream is = sock.getInputStream();
79-
FileOutputStream fos = new FileOutputStream(fileName);
80-
BufferedOutputStream bos = new BufferedOutputStream(fos);
81-
int bytesRead = 0;
82-
int current = 0;
83-
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
84-
bos.write(mybytearray, 0, bytesRead);
85-
current = current + bytesRead;
86-
System.out.print(".");
87-
}
88-
System.out.println();
89-
System.out.println("Done!");
90-
oos.close();
91-
ois.close();
92-
bos.close();
93-
}else{
94-
System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
95-
}
96-
*/ oos.close();
97-
sock.close();
38+
Client c = new Client(dirName, serverIP, PORT_NUMBER);
39+
c.runClient();
9840
}
9941

10042
// Process all files and directories under dir

0 commit comments

Comments
 (0)