-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
156 lines (140 loc) · 4.33 KB
/
Server.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
package Chatbotproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server implements Runnable
{
private ArrayList<Connectionhandler> connection;
private ServerSocket server;
private boolean done;
private ExecutorService pool;
public Server()
{
connection = new ArrayList<>();
done = false;
}
@Override
public void run()
{
try {
server = new ServerSocket(9999);
pool = Executors.newCachedThreadPool();
while(!done) {
Socket client = server.accept();
Connectionhandler handler = new Connectionhandler(client);
connection.add(handler);
pool.execute(handler);
}
} catch (Exception e) {
shutdown();
}
}
public void broadcastMessage(String message)
{
for(Connectionhandler ch : connection)
{
if(ch != null){
ch.sendMessage(message);
}
}
}
public void shutdown()
{
done= true;
pool.shutdown();
if(!server.isClosed()){
try {
server.close();
} catch (IOException e) {
// ignore
}
for (Connectionhandler ch : connection)
{
ch.shutdown();
}
}
}
class Connectionhandler implements Runnable
{
private Socket client;
private BufferedReader in;
private PrintWriter out;
private String nickname;
public Connectionhandler(Socket client)
{
this.client=client;
}
@Override
public void run()
{
try
{
out = new PrintWriter(client.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out.println("Please Enter Nick name : ");
nickname = in.readLine();
System.out.println(nickname + " Connected!");
broadcastMessage(nickname + " joined the Chat!");
String message;
while((message = in.readLine()) != null)
{
if(message.startsWith("/nickname "))
{
String[] messageSplit = message.split(" ", 2);
if(messageSplit.length == 2)
{
broadcastMessage(nickname + " has renamed to "+ messageSplit[1]);
System.out.println(nickname + " has renamed to "+ messageSplit[1]);
nickname=messageSplit[1];
out.println("Successfully changed the Nickname to "+nickname);
}else
{
out.println("No Nickname provided!");
}
}
else if(message.startsWith("/quit"))
{
broadcastMessage(nickname + " has left the chat!");
shutdown();
}
else
{
broadcastMessage( nickname + ": "+message);
}
}
}
catch(IOException e)
{
shutdown();
}
}
public void sendMessage(String message)
{
out.println(message);
}
public void shutdown()
{
try{
in.close();
out.close();
if (!client.isClosed()) {
client.close();
}
}catch (IOException e)
{
// ignore
}
}
}
public static void main(String[] args)
{
Server server = new Server();
server.run();
}
}