-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatServer.java
175 lines (142 loc) · 4.62 KB
/
ChatServer.java
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
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer{
int userNumber = 0;
ArrayList<PrintWriter> alpw = new ArrayList<PrintWriter>();
Socket controlSocket;
public PrintWriter wri;
boolean keepGoing = true;
public static void main(String [] args) {
ChatServer t1 = new ChatServer();
}
public ChatServer()
{
ServerSocket socket = null;
try {
System.out.println("getLocalHost: "+InetAddress.getLocalHost() );
System.out.println("getByName: "+InetAddress.getByName("localhost") );
socket = new ServerSocket(15000);
controlSocket = null;
while(true){ // run forever once up
controlSocket = socket.accept(); // wait for connection
ThServer createThread = new ThServer( controlSocket );
createThread.start();
}
}
catch( BindException be ) {
System.out.println("Server already running on this computer, stopping.");
}
catch( IOException ioe ) {
System.out.println("IO Error");
ioe.printStackTrace();
}
}
// public void setFlag()
// {
// if(keepGoing == true)
// {
// keepGoing = false;
// }
// else if(keepGoing == false)
// {
// keepGoing = true;
// }
//
//
// }
public boolean getFlag()
{
return keepGoing;
}
class ThServer extends Thread {
Socket cs;
String name;
int count = 0;
String username;
BufferedReader reader;
PrintWriter writer;
String exit = "The Server is offline";
boolean Going;
boolean nameSet = false;
public ThServer( Socket cs ) {
this.cs = cs;
}
public void run() {
Runtime.getRuntime().addShutdownHook(
new Thread(
new Runnable() {
@Override
public void run()
{
try
{
writer.println(exit);
writer.flush();
}
catch(Exception sda){}
}}));
String message = "";
try {
reader = new BufferedReader(
new InputStreamReader(
cs.getInputStream()));
writer = new PrintWriter(
new OutputStreamWriter(
cs.getOutputStream()));
alpw.add(writer);
}
catch(Exception e ) {
System.out.println("Error in Thread");
e.printStackTrace();
}
Going = getFlag();
while(Going == true){
if(message != null){
try{
message = reader.readLine(); // from client
if(message.length()>0)
{
if(count == 0){
username = message;
String join = username + " joined the chat!";
System.out.println(join);
nameSet = true;
for(PrintWriter pw : alpw){
pw.println(join);
pw.flush();
}
count ++;
userNumber++;
}
else if(count == 1){
String sendMess = username + ": " + message;
System.out.println(sendMess);
for(PrintWriter pw : alpw){
pw.println(sendMess);
pw.flush();
}
}
}
}
catch(Exception e){
String left = username + " left the chat";
System.out.println(left);
for(PrintWriter pw : alpw){
pw.println(left);
pw.flush();
}
try{
cs.close();
break;
}
catch(Exception ex)
{
System.out.println("Error closing error.");
}
}
}
}
}
}
}