Skip to content

Commit c7ec422

Browse files
author
quaa
committed
Created new class for server
1 parent d0398f4 commit c7ec422

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/Server.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.File;
2+
import java.io.ObjectInputStream;
3+
import java.net.ServerSocket;
4+
import java.net.Socket;
5+
6+
7+
public class Server {
8+
private int PORT_NUMBER;
9+
10+
public Server(int port) {
11+
PORT_NUMBER = port;
12+
}
13+
14+
public void startServer() throws Exception {
15+
System.out.println("Starting File Sync Server!");
16+
17+
ServerSocket servsock = new ServerSocket(PORT_NUMBER);
18+
19+
while (true) {
20+
Socket sock = servsock.accept();
21+
22+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
23+
String baseDir = (String) ois.readObject();
24+
25+
File fBaseDir = new File(baseDir);
26+
27+
if(fBaseDir.exists() && fBaseDir.isDirectory()){
28+
System.out.println(baseDir + " is a directory and exists!");
29+
/* System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
30+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
31+
oos.writeObject("1");
32+
byte[] mybytearray = new byte[(int) f.length()];
33+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
34+
bis.read(mybytearray, 0, mybytearray.length);
35+
OutputStream os = sock.getOutputStream();
36+
os.write(mybytearray, 0, mybytearray.length);
37+
os.flush();
38+
System.out.println(" DONE!");
39+
*/ }else{
40+
System.out.println(baseDir + " Exists?: " + fBaseDir.exists() + " IsDirectory?: " + fBaseDir.isDirectory());
41+
/* System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
42+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
43+
oos.writeObject("0");
44+
oos.flush();
45+
*/ }
46+
sock.close();
47+
}
48+
}
49+
}

src/javaFileSync.java

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,8 @@ public static void main(String[] args) {
3030
}
3131

3232
public static void server() throws Exception {
33-
System.out.println("Starting File Sync Server!");
34-
35-
ServerSocket servsock = new ServerSocket(PORT_NUMBER);
36-
37-
while (true) {
38-
Socket sock = servsock.accept();
39-
40-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
41-
String baseDir = (String) ois.readObject();
42-
43-
File fBaseDir = new File(baseDir);
44-
45-
if(fBaseDir.exists() && fBaseDir.isDirectory()){
46-
System.out.println(baseDir + " is a directory and exists!");
47-
/* System.out.print("Sending file '" + fileName + "' to: " + sock.getInetAddress() + "...");
48-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
49-
oos.writeObject("1");
50-
byte[] mybytearray = new byte[(int) f.length()];
51-
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
52-
bis.read(mybytearray, 0, mybytearray.length);
53-
OutputStream os = sock.getOutputStream();
54-
os.write(mybytearray, 0, mybytearray.length);
55-
os.flush();
56-
System.out.println(" DONE!");
57-
*/ }else{
58-
System.out.println(baseDir + " Exists?: " + fBaseDir.exists() + " IsDirectory?: " + fBaseDir.isDirectory());
59-
/* System.out.println(sock.getInetAddress() + " requested " + fileName + ", but it does not exist!");
60-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
61-
oos.writeObject("0");
62-
oos.flush();
63-
*/ }
64-
sock.close();
65-
}
33+
Server s = new Server(PORT_NUMBER);
34+
s.startServer();
6635
}
6736

6837
public static void client(String dirName, String serverIP) throws Exception {

0 commit comments

Comments
 (0)