-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonServerMulti.java
More file actions
233 lines (154 loc) · 6.58 KB
/
PersonServerMulti.java
File metadata and controls
233 lines (154 loc) · 6.58 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
229
230
231
232
233
/*
* Cameron Massey 2/16/2016 Java GUI chat room server application
*
*/
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.ArrayList;
public class PersonServerMulti {
private static ArrayList<Person> array = new ArrayList<>();
public static ArrayList<Person> getArray(){
return array;
}
// Listen for incoming connections and handle them
public static void main(String[] args) {
int i=0;
try{
System.out.print("Enter port #: ");
Scanner sc = new Scanner(System.in);
int in = sc.nextInt();
ServerSocket listener = new ServerSocket(in);
InetAddress IP=InetAddress.getLocalHost();
System.out.println(IP.getHostAddress());
Socket client;
while(true){
try{
Thread.sleep(0);//don't remember why you need this but you do
}
catch (Exception e)
{e.printStackTrace();}
//limits the number of users to 10
while(array.size() < 10){
client = listener.accept();
Person p = new Person(client, new PrintStream(client.getOutputStream()));
array.add(p);
doListen listen= new doListen(p);
Thread t = new Thread(listen);
t.start();
} //end while
}//end while
}//end try
catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}//end catch
}
public static int getEmpty(Person [] array2){
int i = 0;
for(Person x : array2){
if(x == null){
return i;
}
i++;
}
return 500;
}
public static boolean hasEmpty(Person [] array2){
boolean x = false;
for(Person p: array2){
if(p == null)
return true;
}
return x;
}//end hasEmpty
public static void writeTo(String input, Person p1){
try{
Person p = null;
for(Person x : array)
{
//all messages have a "code" attached to the front of the string which decides what action to do based on that string. Ex: *removePerson, *main, *personset
if(x != null && x.getIsConnected() == true)
{
//if the code is in the form of: 'name of intended user' + 'message'
if(input.length() > 0 && input.substring(0, x.getName().length()).equals(x.getName())){
x.printOut("Secret from " + p1.getName() + ": " + input.substring(x.getName().length()));
}
if(input.length() > 4 && input.substring(0,5).equals("*main") && p1.getName() != null){
x.printOut(input.substring(0,5) + p1.getName() + ": " + input.substring(5));
}
for(Person y : array){
if(y != null && y.getIsConnected() == false){
x.printOut("*removePerson" + y.getName());
}
if(y != null && y.getName() != null && y.getIsConnected() == true){
x.printOut("*personSet" + y.getName());
}
}//end inner for loop
}
if(x != null && x.getIsConnected() == false){
p = x;
}
}//end for loop
if(p != null){
array.remove(p);
array.trimToSize();
}
}catch (Exception e) {
System.out.println("Exception " + e + " Here!");
}
}
}
//read from client
class doListen implements Runnable {
private Socket client;
private String line,input;
private Person p1;
doListen(Person p1 ) {
this.p1 = p1;
this.client = p1.getSocket();
System.out.println(client);
}
public void run () {
input="";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
//sets name if not already set
if(p1.getName() == null){
Boolean b = false;
do{
String str = "Please enter a unique name: ";
p1.printOut(str);
input = reader.readLine().substring(5);
b = check(input,PersonServerMulti.getArray());
}while(b == false);
p1.setName(input);
}
PersonServerMulti.writeTo("", p1);
//gets client input and prints it
while((line = reader.readLine()) != null) {
input= line;
System.out.println("I got:" + input);
PersonServerMulti.writeTo(input, p1);
}
} catch (Exception e) {
//if someone disconnects set that Persons isConnected value to false
System.out.println("Disconnected " + p1.getName());
p1.setIsConnected(false);
p1.closeOutstream();
//have to send a message so the list updates after disconnection.
PersonServerMulti.writeTo("", p1);
}
}//end run
public Boolean check(String name, ArrayList<Person> array){
boolean uniqueName = true;
for(Person p : array){
if(p != null){
if(name.equals(p.getName())){
uniqueName = false;
}
}
}
return uniqueName;
}
}