-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBankServer.java
More file actions
56 lines (45 loc) · 1.35 KB
/
BankServer.java
File metadata and controls
56 lines (45 loc) · 1.35 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
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import BankClasses.Bank;
import BankClasses.BankApp;
public class BankServer {
private Bank bank;
private ServerSocket serverSocket;
public BankServer(int port) throws IOException {
bank = new Bank("R&S Bank");
serverSocket = new ServerSocket(port);
}
public void startServer() {
BankApp.loadBank(bank);// load some accounts
try {
try { /* allows finally to close socket */
/* log initialization */
System.out.println("*** " + bank.getName() + " Server System ***");
/* listen for new connection forever, pushing each thread into ArrayList */
for (;;) {
System.out.println("listening for a connection...");
Thread t = new ServerThread(serverSocket.accept(), bank);
t.start();
}
} finally {
System.out.println("*** the server is going to stop running ***");
serverSocket.close();
}
} catch (EOFException eof) {
System.out.println("*** THE client has terminated connection ***");
eof.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
/* create new Server */
try {
BankServer serv = new BankServer(Integer.parseInt(args[0]));
serv.startServer();
} catch (IOException e) {
System.out.println(e.getMessage());
}
} // end main
}