-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
168 lines (145 loc) · 4.86 KB
/
ChatServer.java
File metadata and controls
168 lines (145 loc) · 4.86 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
//package chatapplication;
import com.sun.deploy.util.SessionState;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
final class ChatServer {
private static int uniqueId = 0;
private final List<ClientThread> clients = new ArrayList<>();
private final int port;
// Date date = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
private ChatServer(int port) {
this.port = port;
}
/*
* 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 synchronized void start() {
try {
ServerSocket serverSocket = new ServerSocket(port);
while(true) {
Socket socket = serverSocket.accept();
// server waits until a client opens a Socket with the same address and port number
// Socket socket = serverSocket.accept();
ClientThread r = new ClientThread(socket, uniqueId++);
// System.out.println("After connection : "+ uniqueId);
Thread t = new Thread(r);
clients.add(r);
// System.out.println("Client's size: " + clients.size());
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized void broadcast(String message)
{
for(int i = 0; i < clients.size(); i++)
{
clients.get(i).writeMessage(message);
}
}
/*
* > java ChatServer
* > java ChatServer portNumber
* If the port number is not specified 1500 is used
*/
public static synchronized void main(String[] args) {
ChatServer server = new ChatServer(1500);
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();
}
}
private synchronized boolean writeMessage(String msg) {
//msg = cm.getMessage();
if(socket.isConnected())
{
try {
System.out.print(username+": "+ msg);
sOutput.writeObject(msg);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
{
return false;
}
}
private synchronized void remove(int id)
{
clients.remove(clients.get(id));
}
/*
* This is what the client thread actually runs.
*/
@Override
public void run() {
// Read the username sent to you by client
boolean run = true;
while (run)
{
try {
cm = (ChatMessage) sInput.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
if(cm.getType() == 1)
{
run = false;
try {
socket.close();
sOutput.close();
sInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else {
// System.out.print(username);
// System.out.println(cm.getMessage());
System.out.println(clients.size());
broadcast(cm.getMessage());
// writeMessage(cm.getMessage());
}
// Send message back to the client
/*try {
// sOutput.writeObject(username+": "+cm.getMessage());
} catch (IOException e) {
e.printStackTrace();
}*/
}
}
}
}