-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
212 lines (173 loc) · 6.19 KB
/
Server.java
File metadata and controls
212 lines (173 loc) · 6.19 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
package com.cn;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import com.cn.*;
public class Server {
private static final int sPort = 8000; //The server will be listening on this port number
private static final ArrayList<Integer> clientNumbers=new ArrayList<Integer>();
private static final ArrayList<ObjectOutputStream> clientStreams = new ArrayList<ObjectOutputStream>();
public static void main(String[] args) throws Exception {
System.out.println("The server is running.");
ServerSocket listener = new ServerSocket(sPort);
int clientNum = 1;
try {
while(true) {
clientNumbers.add(clientNum);
new Handler(listener.accept(),clientNum).start();
System.out.println("Client " + clientNum + " is connected!");
clientNum++;
}
} finally {
listener.close();
}
}
/**
* A handler thread class. Handlers are spawned from the listening
* loop and are responsible for dealing with a single client's requests.
*/
private static class Handler extends Thread {
private String message; //message received from the client
private Socket connection;
private ObjectInputStream in; //stream read from the socket
private ObjectOutputStream out; //stream write to the socket
private int no; //The index number of the client
private MessageType messageType;
public Handler(Socket connection, int no) {
this.connection = connection;
this.no = no;
}
public void run() {
try{
//initialize Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
clientStreams.add(out);
out.flush();
in = new ObjectInputStream(connection.getInputStream());
TimeUnit.SECONDS.sleep(2);
while(true){
try{
String message = (String)in.readObject();
messageType = new MessageType(message);
messageSender(this.no,clientStreams,messageType.parseFirstMessage());
}catch(Exception e){
}
}
}
catch(IOException | InterruptedException ioException){
System.out.println("Disconnect with Client " + no);
}
finally{
//Close connections
try{
in.close();
out.close();
connection.close();
}
catch(IOException ioException){
System.out.println("Closing connection Disconnect with Client " + no);
}
}
}
private void messageSender(int no,ArrayList<ObjectOutputStream> clientstreams, String[] messageType) {
if(messageType.length==1){
//broadcasting all messages
System.out.println("client"+no+" broadcasted message");
int index = no-1;
messageType[0]= messageType[0]+no;
for(ObjectOutputStream o: clientStreams){
if(o!=clientStreams.get(index))
sendMessage(o,messageType[0]);
}
}
else if(messageType.length==2 && messageType[1].equals("file")){
//System.out.println("file sender");
//System.out.println(messageType[0]);
System.out.println("client"+no+" broadcasted file");
int index = no-1;
try {
Path path = Paths.get(messageType[0]);
byte[] content = Files.readAllBytes(path);
for(ObjectOutputStream o: clientStreams){
if(o!=clientStreams.get(index)){
int receiverNum = clientStreams.indexOf(o)+1;
sendFile(o, new FileHelper(content,messageType[0],receiverNum,no));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(messageType.length==3){
if(messageType[2].equals("unicast")){
//unicasting message
String clientName = messageType[1];
char lastChar = clientName.charAt(clientName.length()-1);
int clientNo = Character.getNumericValue(lastChar);
System.out.println("client"+no+" unicasted message to client"+clientNo);
int index = clientNo-1;
messageType[0]+=no;
sendMessage(clientStreams.get(index),messageType[0]);
}
else{
//blockcasting message
String clientName = messageType[1];
char lastChar = clientName.charAt(clientName.length()-1);
int clientNo = Character.getNumericValue(lastChar);
int index = clientNo-1;
System.out.println("client"+no+" blockcasted message to client"+clientNo);
int index2 = no-1;
messageType[0]+=no;
for(ObjectOutputStream o :clientStreams){
if(o!=clientStreams.get(index) && o!=clientStreams.get(index2)){
sendMessage(o, messageType[0]);
}
}
}
}
else if(messageType.length==4){
//unicasting file
try {
Path path = Paths.get(messageType[0]);
byte[] content = Files.readAllBytes(path);
String clientName = messageType[1];
//System.out.println("name: "+clientName);
char lastChar = clientName.charAt(clientName.length()-1);
int clientNo = Character.getNumericValue(lastChar);
//System.out.println("no. :"+clientNo);
int index = clientNo-1;
System.out.println("client"+no+" unicasted file to client"+clientNo);
sendFile(clientStreams.get(index), new FileHelper(content,messageType[0],index+1,no));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void sendMessage(ObjectOutputStream out, String message2){
try{
//System.out.println("message: "+message2);
out.writeObject(message2);
out.flush();
}catch(Exception e){
}
}
private void sendFile(ObjectOutputStream out, FileHelper helper){
//System.out.println("Length of file: "+helper.getContent().length);
try{
out.writeObject(helper);
out.flush();
}catch(IOException e){
}
}
}
}