-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
126 lines (100 loc) · 4.23 KB
/
Server.java
File metadata and controls
126 lines (100 loc) · 4.23 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
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
public class Server {
static Vector<ClientHandler> ClientsVec = new Vector<>();
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(9999);
System.out.println("The Server is waiting for the client...");
// running infinite loop for getting
// client request
while (true) {
try {
// socket object to receive incoming client requests
Socket SocServer = server.accept();
System.out.println("Start Connection");
System.out.println("The client with port number = " + SocServer.getPort() + " Added to the server");
// obtaining input and out streams
System.out.println("Waiting for the ID of the Client");
ObjectInputStream ServerInput = new ObjectInputStream(SocServer.getInputStream());
ObjectOutputStream ServerOutput = new ObjectOutputStream(SocServer.getOutputStream());
String id = ServerInput.readUTF();
System.out.println("Client ID : " + id);
ClientHandler ClientsHand = new ClientHandler(SocServer, id, ServerInput, ServerOutput);
// Create a new Thread with this object.
Thread thread = new Thread(ClientsHand);
System.out.println("Adding this client to active client list");
// add this client to active clients list
ClientsVec.add(ClientsHand);
// start the thread.
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class ClientHandler implements Runnable {
static Vector<ClientHandler> NEWClientsVec = new Vector<>();
final ObjectInputStream input;
final ObjectOutputStream output;
public String ID;
Socket s;
// Constructor
public ClientHandler(Socket s, String ID, ObjectInputStream input, ObjectOutputStream output) {
this.ID = ID;
this.s = s;
this.input = input;
this.output = output;
}
public void run() {
try {
while (true) {
System.out.println("The Client Sent this Text to share..");
// receive "share"
String received = input.readUTF();
if (received.equals("Share")) {
// receive the IDs
received = input.readUTF();
NEWClientsVec.clear();
String[] IDs = received.split("\\-");
for (String s : IDs) {
for (ClientHandler mc : Server.ClientsVec) {
if (s.equals(mc.ID) && !mc.ID.equals(this.ID)) {
NEWClientsVec.add(mc);
break;
}
}
// when the the other clients want to edit show this at the parent
}
NEWClientsVec.add(this);
}
while (true) {
Boolean Check = null;
for(ClientHandler mc : NEWClientsVec){
if(ID.equals(mc.ID)){
Check = true;
break;
}
else{
Check = false;
}
}
received = input.readUTF();
for (ClientHandler mc : NEWClientsVec) {
if (!ID.equals(mc.ID)&& Check == true) {
mc.output.writeUTF(received);
mc.output.flush();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}