-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
228 lines (196 loc) · 7.27 KB
/
Server.java
File metadata and controls
228 lines (196 loc) · 7.27 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
import java.net.Socket;
import java.net.SocketException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.ImageIcon;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Server {
final static int PORT = 2000;
static ArrayList<Socket> chatClientList;
static ArrayList<Socket> videoClientList;
static ArrayList<ObjectOutputStream> audioClientList;
static String ENCRYPTED_SECRET_STRING;
public static void main(String[] args) {
chatClientList = new ArrayList<Socket>();
videoClientList = new ArrayList<Socket>();
audioClientList = new ArrayList<ObjectOutputStream>();
int connectedClients = 0;
ServerSocket chatServerSocket, videoServerSocket, audioServerSocket;
Scanner scan = new Scanner(System.in);
System.out.print("Enter The Group Name :");
String groupName = scan.nextLine();
scan.close();
try {
chatServerSocket = new ServerSocket(PORT);
videoServerSocket = new ServerSocket(PORT + 1);
audioServerSocket = new ServerSocket(PORT + 2);
new VideoServer(videoServerSocket).start();
new AudioServer(audioServerSocket).start();
System.out.println("Server Created with Port No: 2000 and Listening ...");
while (true) {
Socket client = chatServerSocket.accept();
DataOutputStream dout = new DataOutputStream(client.getOutputStream());
dout.writeUTF(groupName);
connectedClients++;
if (connectedClients == 1) {
dout.writeUTF("RequestSecretText");
ENCRYPTED_SECRET_STRING = new DataInputStream(client.getInputStream()).readUTF();
} else {
dout.writeUTF(ENCRYPTED_SECRET_STRING);
}
System.out.println("Accecpted new Client into the Server ");
// System.out.println("Total Number of Connected Client :" + connectedClients);
Server.chatClientList.add(client);
new ClientListenThread(client).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ClientListenThread extends Thread {
Socket s;
ClientListenThread(Socket s) {
this.s = s;
}
public void run() {
try {
DataInputStream din = new DataInputStream(s.getInputStream());
while (true) {
String str = din.readUTF();
if (str.startsWith("END")) {
s.close();
break;
} else if (str.startsWith("FILE_TRANS")) {
byte bytes[] = new byte[Integer.parseInt(str.split(":::")[2])];
din.readFully(bytes, 0, bytes.length);
for (Socket ss : Server.chatClientList) {
if (ss == s)
continue;
DataOutputStream dout = new DataOutputStream(ss.getOutputStream());
dout.writeUTF(str);
dout.write(bytes, 0, bytes.length);
dout.flush();
}
continue;
}
for (Socket s : Server.chatClientList) {
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF(str);
}
}
} catch (SocketException e) {
System.out.println("Person Disconnected");
} catch (Exception e) {
e.printStackTrace();
}
int i = Server.chatClientList.indexOf(s);
Server.chatClientList.remove(i);
}
}
class VideoServer extends Thread {
ServerSocket videoServerSocket;
VideoServer(ServerSocket ss) {
videoServerSocket = ss;
}
public void run() {
while (true) {
try {
Socket socket = videoServerSocket.accept();
Server.videoClientList.add(socket);
new VideoStreamThread(socket).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class VideoStreamThread extends Thread {
Socket s;
VideoStreamThread(Socket socket) {
s = socket;
}
public void run() {
try {
ImageIcon ic;
ObjectInputStream oin = new ObjectInputStream(s.getInputStream());
while (true) {
ic = (ImageIcon) oin.readObject();
if (ic != null && ic.getDescription() != null && ic.getDescription().equals("END")) {
System.out.println("end recevied");
s.close();
break;
} else {
for (Socket c : Server.videoClientList) {
// if(c==s) continue;
ObjectOutputStream oout = new ObjectOutputStream(c.getOutputStream());
oout.writeObject(ic);
oout.flush();
}
if (ic != null && ic.getDescription() != null && ic.getDescription().equals("END_VIDEO")) {
oin = new ObjectInputStream(s.getInputStream());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
int i = Server.videoClientList.indexOf(s);
Server.videoClientList.remove(i);
}
}
class AudioServer extends Thread {
ServerSocket audioServerSocket;
AudioServer(ServerSocket ss) {
audioServerSocket = ss;
}
public void run() {
try {
while (true) {
Socket s = audioServerSocket.accept();
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
Server.audioClientList.add(out);
new AudioStreamThread(s, out).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class AudioStreamThread extends Thread {
Socket socket;
private ObjectInputStream ois;
private ObjectOutputStream out;
AudioStreamThread(Socket s, ObjectOutputStream ot) {
socket = s;
out = ot;
}
public void run() {
try {
ois = new ObjectInputStream(socket.getInputStream());
byte[] data = new byte[1024];
while (true) {
int dsize = ois.read(data);
if (dsize == 1024) {
for (ObjectOutputStream oout : Server.audioClientList) {
oout.write(data, 0, dsize);
oout.reset();
}
} else if (dsize == 512) {
System.out.println("[ SERVER ] : dsize-" + dsize + " Client Stopped.");
ois = new ObjectInputStream(socket.getInputStream());
}
}
} catch (SocketException e) {
System.out.println("Person Disconnected");
} catch (Exception e) {
System.out.println(e);
}
int i = Server.audioClientList.indexOf(out);
Server.audioClientList.remove(i);
}
}