-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
314 lines (293 loc) · 12.1 KB
/
Server.java
File metadata and controls
314 lines (293 loc) · 12.1 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
/*
* A chat server that delivers public and private messages.
*/
public class Server {
// The server socket.
private static ServerSocket serverSocket = null;
// The client socket.
private static Socket clientSocket = null;
// This chat server can accept up to maxClientsCount clients' connections.
private static final int maxClientsCount = 10;
private static final clientThread[] threads = new clientThread[maxClientsCount];
public static void main(String args[]) {
// The default port number.
int portNumber = 2222;
if (args.length < 1) {
System.out.println("Usage: java MultiThreadChatServerSync <portNumber>\n"
+ "Now using port number=" + portNumber);
} else {
portNumber = Integer.valueOf(args[0]).intValue();
}
/*
* Open a server socket on the portNumber (default 2222). Note that we can
* not choose a port less than 1023 if we are not privileged users (root).
*/
try {
serverSocket = new ServerSocket(portNumber);
} catch (IOException e) {
System.out.println(e);
}
/*
* Create a client socket for each connection and pass it to a new client
* thread.
*/
while (true) {
try {
clientSocket = serverSocket.accept();
int i = 0;
for (i = 0; i < maxClientsCount; i++) {
if (threads[i] == null) {
(threads[i] = new clientThread(clientSocket, threads)).start();
break;
}
}
if (i == maxClientsCount) {
PrintStream os = new PrintStream(clientSocket.getOutputStream());
os.println("Server too busy. Try later.");
os.close();
clientSocket.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
/*
* The chat client thread. This client thread opens the input and the output
* streams for a particular client, ask the client's name, informs all the
* clients connected to the server about the fact that a new client has joined
* the chat room, and as long as it receive data, echos that data back to all
* other clients. The thread broadcast the incoming messages to all clients and
* routes the private message to the particular client. When a client leaves the
* chat room this thread informs also all the clients about that and terminates.
*/
class clientThread extends Thread {
private String clientName = null;
private DataInputStream is = null;
private PrintStream os = null;
private Socket clientSocket = null;
private final clientThread[] threads;
private int maxClientsCount;
public clientThread(Socket clientSocket, clientThread[] threads) {
this.clientSocket = clientSocket;
this.threads = threads;
maxClientsCount = threads.length;
}
@SuppressWarnings("deprecation")
public void run() {
int maxClientsCount = this.maxClientsCount;
clientThread[] threads = this.threads;
try {
/*
* Create input and output streams for this client.
*/
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
String name;
while (true) {
os.println("Enter name");
name = is.readLine().trim();
System.out.println("Client " + name + " connected");
if (name!=null) {
break;
}
}
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] == this) {
clientName = name;
break;
}
}
}
/* Start the conversation. */
while (true) {
String line = is.readLine();
if (line.startsWith("/quit")) {
break;
}
/* If the message is private sent it to the given client. */
if (line.startsWith("unicast")) {
String[] words = line.split("\\s", 0);
if (words.length > 1 && words[1].equals("message")) {
//words[1] = words[1].trim();
if (!words[words.length-1].isEmpty()) {
synchronized (this) {
String msg = "";
for(int j = 2; j < words.length-1;j++)
{
msg = msg + words[j];
}
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null
&& threads[i].clientName.equals(words[words.length-1])) {
threads[i].os.println("@" + name + ": " + msg);
/*
* Echo this message to let the client know the private
* message was sent.
*/
// this.os.println(">" + name + "> " + words[1]);
break;
}
}
System.out.println(name +" unicast message to " + words[words.length-1]);
}
}
}else{
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null
&& threads[i].clientName.equals(words[words.length-1])) {
int index = words[2].lastIndexOf("/");
String filename = words[2].substring(index+1);
FileInputStream in = new FileInputStream(words[2]);
String target = "../"+threads[i].clientName+"/"+filename;
FileOutputStream out = new FileOutputStream(target);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
in.close();
out.close();
threads[i].os.println("File " + filename + " was sent by " + name);
}
}
System.out.println(name +" unicast file to " + words[words.length-1]);
}
}
}else if (line.startsWith("broadcast")){
/* The message is public, broadcast it to all other clients. */
String[] words = line.split("\\s", 3);
if (words.length > 1 && words[1].equals("message") && words[2] != null) {
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i].clientName != null && threads[i] != this) {
threads[i].os.println("@" + name + ": " + words[2]);
}
}
System.out.println(name + " broadcasted message");
}
}
else{
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i].clientName != null && threads[i] != this) {
int index = words[2].lastIndexOf("/");
String filename = words[2].substring(index+1);
FileInputStream in = new FileInputStream(words[2]);
String target = "../"+threads[i].clientName+"/"+filename;
FileOutputStream out = new FileOutputStream(target);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
in.close();
out.close();
threads[i].os.println("File " + filename + " was sent by " + name);
}
}
System.out.println(name + " broadcasted file");
}
}
}
else{
String[] words = line.split("\\s", 0);
if (words.length > 1 && words[1].equals("message")) {
//words[1] = words[1].trim();
if (!words[words.length-1].isEmpty()) {
synchronized (this) {
String msg = "";
for(int j = 2; j < words.length-1;j++)
{
msg = msg + " " +words[j];
}
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null
&& !(threads[i].clientName.equals(words[words.length-1]))) {
threads[i].os.println("@" + name + ": " + msg);
/*
* Echo this message to let the client know the private
* message was sent.
*/
break;
}
}
System.out.println(name + " blockcast message excluding " + words[words.length-1]);
}
}
}
else{
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null
&& !(threads[i].clientName.equals(words[words.length-1]))) {
int index = words[2].lastIndexOf("/");
String filename = words[2].substring(index+1);
FileInputStream in = new FileInputStream(words[2]);
String target = "../"+threads[i].clientName+"/"+filename;
FileOutputStream out = new FileOutputStream(target);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
threads[i].os.println("File " + filename + " was sent by " + name);
}
}
System.out.println(name + " blockcast file excluding " + words[words.length-1]);
}
}
}
}
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null) {
threads[i].os.println("*** The user " + name
+ " is leaving the chat room !!! ***");
}
}
}
os.println("*** Bye " + name + " ***");
/*
* Clean up. Set the current thread variable to null so that a new client
* could be accepted by the server.
*/
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] == this) {
threads[i] = null;
}
}
}
/*
* Close the output stream, close the input stream, close the socket.
*/
is.close();
os.close();
clientSocket.close();
} catch (IOException e) {
}
}
}