-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
154 lines (135 loc) · 4.45 KB
/
ChatServer.java
File metadata and controls
154 lines (135 loc) · 4.45 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
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
final class ChatServer {
private static int uniqueId = 0;
private final List<ChatServer.ClientThread> clients = new ArrayList<>();
private final int port;
private ChatServer(int port) {
this.port = port;
}
private ChatServer() {
this.port = 1500;
}
/*
* This is what starts the ChatServer.
* Right now it just creates the socketServer and adds a new ClientThread to a list to be handled
*/
private void start() {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
Runnable r = new ChatServer.ClientThread(socket, uniqueId++);
Thread t = new Thread(r);
clients.add((ChatServer.ClientThread) r);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* > java ChatServer
* > java ChatServer portNumber
* If the port number is not specified 1500 is used
*/
public static void main(String[] args) {
if (args.length == 0) {
ChatServer server = new ChatServer();
server.start();
}
if (args.length == 1) {
ChatServer server = new ChatServer(Integer.parseInt(args[0]));
server.start();
}
}
/*
* This is a private class inside of the ChatServer
* A new thread will be created to run this every time a new client connects.
*/
private final class ClientThread implements Runnable {
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
int id;
String username;
ChatMessage cm;
private ClientThread(Socket socket, int id) {
this.id = id;
this.socket = socket;
try {
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
username = (String) sInput.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/*
* This is what the client thread actually runs.
*/
@Override
public void run() {
// Read the username sent to you by client
try {
cm = (ChatMessage) sInput.readObject();
if (cm.getType() == 0) {
broadcast(this.username + ": " + cm.getMessage());
} else {
broadcast(this.username + "has logged out.");
remove(this.id);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
// Send message back to the client
try {
sOutput.writeObject(cm.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized void broadcast(String message) { //prints message to terminal of every user, plus server terminal
for (ClientThread client : clients) {
if (client.id != this.id) {
client.writeMessage(message);
}
}
System.out.println(message);
}
public boolean writeMessage(String message) { //writes message to user's terminal
if (socket.isConnected()) {
try {
this.sOutput.writeObject(message);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
public synchronized void remove(int id) { //removes clients from arraylist
clients.remove(this.id);
}
private void close() {
for (ClientThread cl : clients) {
try {
cl.sInput.close();
cl.sOutput.close();
cl.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}