/
Server.java
115 lines (101 loc) · 2.78 KB
/
Server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package tcp.objectserver;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
/**
* A general object-based server. Creates a thread to handle each client. This
* class should be improved to limit the number of threads using a thread pool.
*
* @author amit
*
*/
public class Server {
private long clientNum = 1;
private int port = 0;
public Server(int port) {
this.port = port;
}
/**
* The main method for the server -- runs forever or until the client closes
* the connection.
*
* @throws IOException
*/
public void runServer() throws IOException
{
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server: ready");
while (true) {
new ServerConnection(serverSocket.accept(), clientNum).start();
System.out.println("Server: Started client#" + clientNum);
clientNum++;
}
}
/**
* A thread that handles one client.
*/
private class ServerConnection extends Thread {
private Socket client;
private long clientNum;
ServerConnection(Socket client, long requestNum) throws SocketException {
this.clientNum = requestNum;
this.client = client;
setPriority(NORM_PRIORITY - 1);
}
public void run()
{
try {
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
System.out
.println("ServerConnection: Received connect from " + client.getInetAddress().getHostAddress());
while (true) {
out.writeObject(processRequest(in.readObject()));
out.flush();
}
} catch (EOFException e) { // Normal EOF
try {
client.close();
System.out.println("ServerConnection: Finished client#" + clientNum);
} catch (IOException e1) {
System.err.println(e1);
}
} catch (IOException e) {
System.err.println("I/O error " + e); // I/O error
} catch (ClassNotFoundException e) {
System.err.println(e); // Unknown type of request object
}
}
/**
* Handles one request from a client.
*
* @param request
* A request object from the client.
* @return
*/
private Object processRequest(Object request)
{
if (request instanceof DateRequest)
return new java.util.Date();
else if (request instanceof WorkRequest)
return ((WorkRequest) request).execute();
else
return null;
}
}
public static void main(String argv[]) throws IOException
{
if (argv.length < 1) {
System.err.println("Usage: java Server <port>");
System.exit(1);
}
int port = Integer.parseInt(argv[0]);
Server objserver = new Server(port);
objserver.runServer();
}
}