-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
200 lines (167 loc) · 5.38 KB
/
Copy pathChatServer.java
File metadata and controls
200 lines (167 loc) · 5.38 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
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Vector;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class ChatServer {
static ArrayList<String> userNames = new ArrayList<String>();
static ArrayList<User> userArray = new ArrayList<User>();
static Vector<ConversationHandler> activeClients = new Vector<>();
static InetAddress IP;
// Assigns PrintWriters to each user that connects to the server, preventing
// server from freezing each time a user sends text
// PrintWriter objects of all the clients
static ArrayList<BufferedWriter> buffWriter = new ArrayList<BufferedWriter>();
static ArrayList<userBuffer> userBuffers = new ArrayList<userBuffer>();
// client counter
static int i = 0;
public static void main(String[] args) {
try {
System.out.println("Waiting for client ...");
ServerSocket ss = new ServerSocket(8000);
while (true) {
Socket sock = ss.accept();
System.out.println("Connection Established");
for (User line : userArray) {
System.out.println("Currently connected users are: ");
System.out.println(line.name);
System.out.println("===============================");
}
ConversationHandler handler = new ConversationHandler(sock, "client" + i);
activeClients.add(handler);
handler.start();
i++;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "PLEASE NOTE: Server Terminated, messages will no longer send!");
}
}
}
class ConversationHandler extends Thread {
Socket sockySock;
BufferedReader in;
BufferedWriter out;
ObjectInputStream userObjectInput;
ObjectOutputStream userObjectOutput;
String user;
String myName;
String ip;
User newUser;
User toAdd;
PrintWriter msgLogs;
static FileWriter fw;
static BufferedWriter bw;
public ConversationHandler(Socket sockySock, String myName) throws IOException {
this.sockySock = sockySock;
this.myName = myName;
fw = new FileWriter("./ChatServer-logs.txt", true);
bw = new BufferedWriter(fw);
msgLogs = new PrintWriter(bw, true);
}
// Contains Thread Logic
public void run() {
try {
in = new BufferedReader(new InputStreamReader(sockySock.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(sockySock.getOutputStream()));
userObjectInput = new ObjectInputStream(sockySock.getInputStream());
userObjectOutput = new ObjectOutputStream(sockySock.getOutputStream());
int count = 0;
while (true) {
if (count > 0) {
out.write("Sorry, this name has been taken!\n");
out.flush();
} else {
out.write("Name is Required!\n");
out.flush();
}
user = in.readLine();
ip = in.readLine();
if (user == null) {
return;
}
// If name doesn't exist, add userName to the ArrayList
if (!ChatServer.userNames.contains(user)) {
toAdd = new User(user, ip);
ChatServer.userArray.add(toAdd);
ChatServer.userNames.add(user);
break;
}
count++;
}
// Send name from server to client
out.write("YOURNAME" + user);
out.newLine();
out.flush();
userObjectOutput.writeObject(ChatServer.userArray);
userObjectOutput.flush();
ChatServer.buffWriter.add(out);
userBuffer ub = new userBuffer(user, out);
ChatServer.userBuffers.add(ub);
for (BufferedWriter writer : ChatServer.buffWriter) {
writer.write("update" + ChatServer.userNames);
writer.newLine();
writer.flush();
}
// Reads message from a client and sends to all other clients
while (true) {
String message = in.readLine();
if (message == null) {
return;
}
if (message.startsWith("@")) {
String privMsg = message.substring(1);
String[] splitMsg = privMsg.split(":");
String privUser = splitMsg[0];
message = splitMsg[1];
msgLogs.println(privUser + ": " + message);
//send to both users...
for (userBuffer bu: ChatServer.userBuffers) {
System.out.println("In for loop of buffers" + bu.getName());
if (bu.getName().equals(privUser)) {
bu.getWriter().write(privUser+ ": "+ message);
bu.getWriter().newLine();
bu.getWriter().flush();
}
}
} else {
msgLogs.println(user + ": " + message);
for (BufferedWriter writer : ChatServer.buffWriter) {
writer.write(user + ": " + message);
writer.newLine();
writer.flush();
}
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Connection Terminated by User: " + user + "\n");
// Remove disconnected user from ArrayList
for (User toRemove : ChatServer.userArray) {
if (toRemove.name == user && ChatServer.userArray.size() > 0) {
(ChatServer.userArray).remove(toRemove);
(ChatClient.userArray).remove(toRemove);
ChatServer.userNames.remove(user);
break;
}
}
if (ChatServer.userArray.size() >= 1) {
System.out.println("Users still connected: ");
for (User usersLeft : ChatServer.userArray) {
System.out.println(usersLeft.name);
System.out.println("=======================");
}
}
System.out.println("Connection Terminated by User: " + user);
}
}
}