1212import java .net .Socket ;
1313
1414public class Client {
15- private String dirName ;
16- private String serverIP ;
17- private String fullDirName ;
18- private int PORT_NUMBER ;
15+ private static String dirName ;
16+ private static String serverIP ;
17+ private static String fullDirName ;
18+ private static int PORT_NUMBER ;
1919 private static final String DONE = "DONE" ;
2020
2121 public Client (String dirName , String fullDirName , String serverIP , int port ) {
22- this .dirName = dirName ;
23- this .serverIP = serverIP ;
24- this .fullDirName = fullDirName ;
22+ Client .dirName = dirName ;
23+ Client .serverIP = serverIP ;
24+ Client .fullDirName = fullDirName ;
2525 PORT_NUMBER = port ;
2626
2727 System .out .println ("Client Selected!" );
@@ -33,15 +33,15 @@ public void runClient() throws Exception {
3333
3434 Socket sock = new Socket (serverIP , PORT_NUMBER );
3535 ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send directory name to server
36- oos .writeChars (dirName );
36+ oos .writeChars (new String ( dirName ) );
3737 oos .flush ();
3838
3939 ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive if this directory exists
4040 Boolean fExists = (Boolean ) ois .readObject ();
4141
4242 visitAllDirsAndFiles (new File (fullDirName ), sock );
4343
44- oos .writeChars (DONE );
44+ oos .writeChars (new String ( DONE ) );
4545 oos .flush ();
4646
4747 if (fExists )
@@ -54,12 +54,28 @@ public void runClient() throws Exception {
5454
5555 // Process all files and directories under dir
5656 public static void visitAllDirsAndFiles (File dir , Socket sock ) throws Exception {
57+ ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send pathName fileName LastModified to server
58+ oos .writeChars (new String (dir .getName ()));
59+ oos .flush ();
60+
61+ ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // wait for server to say ok
62+ ois .readBoolean ();
63+
64+ oos .writeBoolean (new Boolean (dir .isDirectory ()));
65+ oos .flush ();
66+
67+ ois .readBoolean ();
68+
69+ oos .writeChars (new String (dir .getAbsolutePath ().substring ((dir .getAbsolutePath ().indexOf (fullDirName ) + fullDirName .length ()))));
70+ oos .flush ();
71+
72+ ois .readBoolean ();
73+
5774 if (!dir .isDirectory ()) {
58- ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send fileName LastModified to server
59- oos .writeChars (dir .getName () + " " + dir .lastModified ());
75+ oos .writeLong (new Long (dir .lastModified ()));
6076 oos .flush ();
6177
62- ObjectInputStream ois = new ObjectInputStream ( sock . getInputStream ()); // receive SEND or RECEIVE
78+ // receive SEND or RECEIVE
6379 Boolean updateToServer = (Boolean ) ois .readObject (); //if true update server, else update from server
6480
6581 if (updateToServer ) { // send file to server
@@ -76,12 +92,12 @@ public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
7692 } else { // update file from server.
7793 dir .delete (); // first delete the current file
7894
79- oos .writeBoolean (true ); // send "Ready"
95+ oos .writeBoolean (new Boolean ( true ) ); // send "Ready"
8096 oos .flush ();
8197
8298 receiveFile (dir , sock );
8399
84- oos .writeBoolean (true ); // send back ok
100+ oos .writeBoolean (new Boolean ( true ) ); // send back ok
85101 oos .flush ();
86102
87103 Long updateLastModified = (Long ) ois .readObject (); // update the last modified date for this file from the server
@@ -90,20 +106,6 @@ public static void visitAllDirsAndFiles(File dir, Socket sock) throws Exception{
90106 oos .close ();
91107 ois .close ();
92108 }
93-
94- } else {
95- ObjectOutputStream oos = new ObjectOutputStream (sock .getOutputStream ()); // send directory name to server
96- oos .writeChars (dir .getName () + "DiR" );
97- oos .flush ();
98-
99- ObjectInputStream ois = new ObjectInputStream (sock .getInputStream ()); // receive if this directory exists
100- Boolean ok = (Boolean ) ois .readObject ();
101-
102- oos .close ();
103- ois .close ();
104-
105- if (!ok ) // if did not receive an ok back from the server, re-run.
106- visitAllDirsAndFiles (dir , sock );
107109 }
108110
109111/* debug */ System .out .println ("Name: " + dir .getName () + " Dir: " + dir .isDirectory () + " Modified: " + dir .lastModified () + " Size: " + dir .length ());
0 commit comments