-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordsDatabaseServer.java
88 lines (70 loc) · 2.61 KB
/
RecordsDatabaseServer.java
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
80
81
82
83
84
85
86
87
88
/*
* RecordsDatabaseServer.java
*
* The server main class.
* This server provides a service to access the Records database.
*
*
*/
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class RecordsDatabaseServer {
private int thePort = 0;
private String theIPAddress = null;
private ServerSocket serverSocket = null;
//Support for closing the server
//private boolean keypressedFlag = false;
//Class constructor
public RecordsDatabaseServer(){
//Initialize the TCP socket
thePort = Credentials.PORT;
theIPAddress = Credentials.HOST;
//Initialize the socket and runs the service loop
System.out.println("Server: Initializing server socket at " + theIPAddress + " with listening port " + thePort);
System.out.println("Server: Exit server application by pressing Ctrl+C (Windows or Linux) or Opt-Cmd-Shift-Esc (Mac OSX)." );
try {
//Initialize the socket
int maxConnectionQueue = 3;
serverSocket = new ServerSocket(thePort,maxConnectionQueue, InetAddress.getByName(theIPAddress));
System.out.println("Server: Server at " + theIPAddress + " is listening on port : " + thePort);
} catch (Exception e){
//The creation of the server socket can cause several exceptions;
//See https://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html
System.out.println(e);
System.exit(1);
}
}
//Runs the service loop
public void executeServiceLoop()
{
System.out.println("Server: Start service loop.");
try {
//Service loop
while (true) {
Socket aSocket = this.serverSocket.accept();
RecordsDatabaseService serviceThread = new RecordsDatabaseService(aSocket);
}
} catch (Exception e){
//The creation of the server socket can cause several exceptions;
//See https://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html
System.out.println(e);
}
System.out.println("Server: Finished service loop.");
}
/*
@Override
protected void finalize() {
//If this server has to be killed by the launcher with destroyForcibly
//make sure we also kill the service threads.
System.exit(0);
}
*/
public static void main(String[] args){
//Run the server
RecordsDatabaseServer server=new RecordsDatabaseServer(); //inc. Initializing the socket
server.executeServiceLoop();
System.out.println("Server: Finished.");
System.exit(0);
}
}