Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
252 lines (219 loc) · 5.9 KB
/
Copy pathServer.java
File metadata and controls
252 lines (219 loc) · 5.9 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame{
private ServerSocket server;
private int counter = 0;
private boolean start = false;
private String start_fn = new String();
private User user[];
public Server()
{
user = new User[100];
try{
server = new ServerSocket( 12345, 100 );
System.out.println("Waiting for connection.....");
}
catch(Exception excp){
excp.printStackTrace();
}
}
public void runServer()
{
try {
while (counter<100) {
try{
user[counter] = new User(server.accept());
System.out.println("Server is connected to user "+Integer.toString(counter+1));
counter++;
}
catch (EOFException eofException) {
System.err.println( "Server terminated connection" );
}
}
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
public static void main( String args[] )
{
Server application = new Server();
application.runServer();
}
private class User extends Thread{
private Socket connection;
private ObjectOutputStream output,osm;
private ObjectInputStream input,ism,istm;
String type;
public User(Socket socket)
{
connection = socket;
try{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}
catch(IOException ioe){
ioe.printStackTrace();
}
start();
}
public void run()
{
while(true){
MarksTable mt;
AddExamInfo ae;
AddQ qs[];
try{
type = (String)input.readObject();
}
catch(Exception e){
e.printStackTrace();
}
if(type.equals("output")){
try{
String filename = (String)input.readObject();
String m = (String)input.readObject();
if(m.equals("Add_Q_Now")){
ae = (AddExamInfo)input.readObject();
FileOutputStream os = new FileOutputStream(filename);
osm = new ObjectOutputStream(os);
osm.writeObject(ae);
osm.flush();
osm.close();
}
else if(m.equals("marks")){
mt = (MarksTable) input.readObject();
FileOutputStream os = new FileOutputStream(filename);
osm = new ObjectOutputStream(os);
osm.writeObject(mt);
osm.flush();
osm.close();
}
else{
int no_of_obj = Integer.parseInt(m);
ae = (AddExamInfo)input.readObject();
qs = new AddQ[no_of_obj];
for(int p=0; p<no_of_obj; p++){
qs[p] = (AddQ)input.readObject();
}
FileOutputStream os = new FileOutputStream(filename);
osm = new ObjectOutputStream(os);
osm.writeObject(ae);
osm.flush();
for(int p=0; p<no_of_obj; p++){
osm.writeObject(qs[p]);
osm.flush();
}
osm.close();
}
}
catch(Exception e){
e.printStackTrace();
try{
output.writeObject("Error");
output.flush();
}
catch(Exception eee){
eee.printStackTrace();
}
}
}
if(type.equals("input")){
try{
String filename = (String)input.readObject();
String m = (String)input.readObject();
if(m.equals("marks")){
FileInputStream is = new FileInputStream(filename);
ism = new ObjectInputStream(is);
mt=(MarksTable) ism.readObject();
ism.close();
output.writeObject(mt);
output.flush();
}
else if(m.equals("exam")){
FileInputStream is = new FileInputStream(filename);
istm = new ObjectInputStream(is);
ae = (AddExamInfo)istm.readObject();
int a = ae.totalques;
qs = new AddQ[a];
for(int p=0; p<a; p++){
qs[p] = (AddQ)istm.readObject();
}
istm.close();
output.writeObject(ae);
output.flush();
for(int q=0; q<a; q++){
output.writeObject(qs[q]);
output.flush();
}
}
else if(m.equals("info")){
FileInputStream is = new FileInputStream(filename);
istm = new ObjectInputStream(is);
ae = (AddExamInfo)istm.readObject();
istm.close();
output.writeObject(ae);
output.flush();
}
else if(m.equals("Q_request")){
while(!(start && filename.equals(start_fn))){
sleep(1000);
}
FileInputStream is = new FileInputStream(filename);
istm = new ObjectInputStream(is);
ae = (AddExamInfo)istm.readObject();
int a = ae.totalques;
qs = new AddQ[a];
for(int p=0; p<a; p++){
qs[p] = (AddQ)istm.readObject();
}
istm.close();
output.writeObject(ae);
output.flush();
for(int q=0; q<a; q++){
output.writeObject(qs[q]);
output.flush();
}
}
else if(m.equals("start")){
start = true;
start_fn = new String(filename);
FileInputStream is = new FileInputStream(filename);
istm = new ObjectInputStream(is);
ae = (AddExamInfo)istm.readObject();
int delay = ae.time * 1000 * 60;
sleep(delay);
start = false;
start_fn = new String();
}
}
catch(Exception e){
e.printStackTrace();
try{
output.writeObject("Error");
output.flush();
}
catch(Exception eee){
eee.printStackTrace();
}
}
}
if(type.equals("disconnect")){
try {
output.close();
input.close();
connection.close();
break;
}
catch(IOException ioException) {
ioException.printStackTrace();
}
}
}
}
}
}