-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
221 lines (194 loc) · 7.29 KB
/
ChatServer.java
File metadata and controls
221 lines (194 loc) · 7.29 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
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class ChatServer {
protected int serverPort = 1234;
protected static List<Socket> clients = new ArrayList<Socket>(); // list of clients
protected static List<String> usernames = new ArrayList<String>(); // list of client's usernames
public static void main(String[] args) throws Exception {
new ChatServer();
}
public ChatServer() {
ServerSocket serverSocket = null;
// create socket
try {
serverSocket = new ServerSocket(this.serverPort); // create the ServerSocket
} catch (Exception e) {
System.err.println("[system] could not create socket on port " + this.serverPort);
e.printStackTrace(System.err);
System.exit(1);
}
// start listening for new connections
System.out.println("[system] listening ...");
try {
while (true) {
Socket newClientSocket = serverSocket.accept(); // wait for a new client connection
synchronized(this) {
clients.add(newClientSocket); // add client to the list of clients
usernames.add(addUsername(newClientSocket)); // dodam username v seznam
}
ChatServerConnector conn = new ChatServerConnector(this, newClientSocket); // create a new thread for communication with the new client
conn.start(); // run the new thread
}
} catch (Exception e) {
System.err.println("[error] Accept failed.");
e.printStackTrace(System.err);
System.exit(1);
}
// close socket
System.out.println("[system] closing server socket ...");
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
// send a message to all clients connected to the server
public void sendToAllClients(String message) throws Exception {
Iterator<Socket> i = clients.iterator();
while (i.hasNext()) { // iterate through the client list
Socket socket = (Socket) i.next(); // get the socket for communicating with this client
try {
DataOutputStream out = new DataOutputStream(socket.getOutputStream()); // create output stream for sending messages to the client
out.writeUTF(message); // send message to the client
} catch (Exception e) {
System.err.println("[system] could not send message to a client");
e.printStackTrace(System.err);
}
}
}
// send a message to one client, connected to the server
public void sendToOneClient(String message, String username2, Socket socketSender) throws Exception {
Socket socket = clients.get(0);
boolean uspesno = false;
for (int i=0; i<usernames.size(); i++) { // najde socket clienta z uporabniskim imenom username2
if (username2.matches(usernames.get(i))) {
socket = clients.get(i);
uspesno = true;
}
}
try {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
if (uspesno) {
out.writeUTF(message); // send message to the client
if(socketSender != socket) { // sebi ne poslje sporocila 2x
out = new DataOutputStream(socketSender.getOutputStream()); // create output stream for sending messages to the client
out.writeUTF(message);
}
} else {
out.writeUTF("Prišlo je do napake, uporabnik ne obstaja!");
}
} catch (Exception e) {
System.err.println("[system] could not send message to a client");
e.printStackTrace(System.err);
}
}
// send list of usernames currently connected to server
public void sendList(Socket socket) {
String message = "Seznam uporabnikov: \n";
for (int i=0; i<usernames.size(); i++) {
message += i+1 + ") " + usernames.get(i) + "\n";
}
try {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(message);
} catch (Exception e) {
System.err.println("[system] could not send message to a client");
e.printStackTrace(System.err);
}
}
public void removeClient(Socket socket) {
synchronized(this) {
clients.remove(socket);
}
}
public static String addUsername(Socket socket) {
DataInputStream in;
try {
in = new DataInputStream(socket.getInputStream());
String currentUsername = in.readUTF();;
return currentUsername;
} catch (IOException e) {
e.printStackTrace(System.err);
return null;
}
}
public static List<Socket> getClients() {
return clients;
}
public static List<String> getUsernames() {
return usernames;
}
}
// -------------------------------------------------------------------------------//
// ---------------------- CLASS THREAD -------------------------------------------//
class ChatServerConnector extends Thread {
private ChatServer server;
private Socket socket;
protected List<Socket> clients = ChatServer.getClients();
protected List<String> usernames = ChatServer.getUsernames();
static int counter = 0;
String username;
String username2;
public ChatServerConnector(ChatServer server, Socket socket) {
this.server = server;
this.socket = socket;
}
public void run() {
System.out.println("[system] "+ usernames.get(counter)+" connected with " + this.socket.getInetAddress().getHostName() + ":" + this.socket.getPort());
counter++;
DataInputStream in;
try {
in = new DataInputStream(this.socket.getInputStream()); // create input stream for listening for incoming messages
} catch (IOException e) {
System.err.println("[system] could not open input stream!");
e.printStackTrace(System.err);
this.server.removeClient(socket);
return;
}
while (true) { // infinite loop in which this thread waits for incoming messages and processes them
String msg_received;
try {
msg_received = in.readUTF(); // read the message from the client
} catch (Exception e) {
System.err.println("[system] there was a problem while reading message client on port " + this.socket.getPort());
e.printStackTrace(System.err);
this.server.removeClient(this.socket);
return;
}
if (msg_received.length() == 0) // invalid message
continue;
for (int i = 0; i< clients.size() ; i++) { // najde username uporabnika z dolocenim
if (this.socket == clients.get(i))
username = usernames.get(i);
}
System.out.println("[RKchat] ["+ time() + "] [" + username + "] : " + msg_received); // print the incoming message in the console
String msg_send = null; // TODO
try {
if (msg_received.startsWith("/pvt")) { // send message to one client
username2 = msg_received.substring(6, msg_received.length());
username2 = username2.substring(0, username2.indexOf("'"));
msg_received = msg_received.substring(6 + username2.length()+2, msg_received.length()); // odstrani zacetek sporocila: /pvt 'username2'
msg_send = "["+ time() + "] " + "[" + username +"]" + " privately said: " + msg_received.toUpperCase(); // TODO
this.server.sendToOneClient(msg_send, username2, this.socket);
} else if (msg_received.startsWith("/userlist")) {
this.server.sendList(this.socket);
} else {
msg_send = "["+ time() + "] " + "[" + username +"]" + " said: " + msg_received.toUpperCase(); // TODO
this.server.sendToAllClients(msg_send); // send message to all clients
}
} catch (Exception e) {
System.err.println("[system] there was a problem while sending the message to all clients");
e.printStackTrace(System.err);
continue;
}
}
}
public static String time() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
Date date = new Date();
return dateFormat.format(date);
}
}