-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.java
136 lines (90 loc) · 3.69 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*********************
* Socket Server
* @file Server.java
* @author Wangai
**********************/
import java.net.*;
import java.io.*;
public class Server extends Thread {
private ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(1000000);
}
public static void main(String [] args) {
int port = Integer.parseInt(args[0]);
try {
// ? do this in a seperate process?
Thread t = new Server(port);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort());
Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
DataInputStream input = new DataInputStream(server.getInputStream());
DataOutputStream output = new DataOutputStream(server.getOutputStream());
// initial interaction ** send menu
output.writeUTF("\n\nSERVER>\n" + menu());
int service = input.readInt();
int input1 = input.readInt();
int input2 = input.readInt();
/*
System.out.println("service: " + service);
System.out.println("input1: " + input1);
System.out.println("input2: " + input2);
*/
do {
// ** evaluate request
if (service == 0) {
break; // from do while
} else if (service == 1) {
output.writeUTF("\n\nSERVER>\n" + menu());
} else if (service == 2) {
output.writeUTF("SERVER>\n\t " + add(input1, input2));
} else if (service == 3) {
output.writeUTF("SERVER>\n\t " + diff(input1, input2));
} else if (service == 4) {
output.writeUTF("SERVER>\n\t " + mult(input1, input2));
} else if (service == 5) {
output.writeUTF("SERVER>\n\t " + qout(input1, input2));
} else {
output.writeUTF("SERVER>\n\t " + "invalid choice");
}
service = input.readInt();
input1 = input.readInt();
input2 = input.readInt();
} while (service != 0);
output.writeUTF("SERVER>\n\t " + "Thank you for connecting to " + server.getLocalSocketAddress());
server.close();
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
private String menu() {
return "\tMath Server\n***************************\nchoose a number for the coresponding service\nthen send response in this format\n\n\tservice: (int)\n\tinput1: (int)\n\tinput2: (int)\n\n 0. Quit\n 1. Print this help message\n 2. Addition\n 3. Subtraction\n 4. Multiplication\n 5. Division";
}
private int add(int a, int b) {
return a + b;
}
private int diff(int a, int b) {
return a - b;
}
private int mult(int a, int b) {
return a*b;
}
private double qout(int a, int b) {
return (double)a/b;
}
}