-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
149 lines (133 loc) · 5.13 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
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.SimpleDateFormat;
import static java.lang.System.out;
public class Server{
Vector<String> users = new Vector<String>();
Vector<HandleClient> clients = new Vector<HandleClient>();
Vector<Socket> sockets = new Vector<Socket>();
private static File chatlogs = new File("chatlogs.txt");
private static int logsCount = 0;
public void process() throws Exception{
ServerSocket server = new ServerSocket(8080, 2);
out.println("Server started!");
out.println("IP Address: localhost");
out.println("Port: 8080");
while(true){
Socket client = server.accept();
sockets.add(client);
HandleClient c = new HandleClient(client);
clients.add(c);
}
}
public static void main(String ... args) throws Exception{
new Server().process();
chatlogs.createNewFile();
}
public void broadcast(String user, String message) throws IOException{
for(HandleClient c: clients){
if(!c.getUsername().equals(user)){
c.sendMessage(user, message);
}
else{
c.sendMessage(user, message);
}
}
if(message != null){
char[] msg = message.toCharArray();
String time = new SimpleDateFormat("MM-dd-yyyy_HH:mm:ss").format(Calendar.getInstance().getTime());
String other = "";
for(HandleClient c: clients){
if(!c.getUsername().equals(user)){
other = c.getUsername();
}
}
switch(msg[1]) {
case '1':
out.println("[" + user + "] has logged in at " + time);
break;
case '2':
out.println("[" + user + "] has disconnected at " + time);
int toRemove = users.indexOf(user);
users.remove(new String(user));
clients.remove(toRemove);
sockets.remove(toRemove);
break;
case '3':
if(clients.size() == 1){
out.println("[" + user + "] has tried to send a message at " + time);
}
else{
out.println("[" + user + "] has sent a message to " + other + " at " + time);
}
break;
case '4':
if(clients.size() == 1){
out.println("[" + user + "] has tried to send a message at " + time);
}
else{
out.println("[" + user + "] has sent a file to " + other + " at " + time);
}
break;
default:
out.println("Case not found " + time);
}
}
}
class HandleClient extends Thread{
String name = "";
BufferedReader input;
PrintWriter output;
public HandleClient(Socket client) throws Exception{
// get input and output streams
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
output = new PrintWriter(client.getOutputStream(), true);
// read name
name = input.readLine();
users.add(name); // add to vector
start();
}
public void sendMessage(String username, String msg) throws IOException{
String line = username + ": " + msg.substring(3);
if(clients.size() == 1){
output.println("Hello, " + username + "! Let's wait for another client to connect for you to be able to send messages.");
}
else{
output.println(line);
}
if(clients.size() == 1){
Writer output;
String time = new SimpleDateFormat("MM-dd-yyyy_HH:mm:ss").format(Calendar.getInstance().getTime());
output = new BufferedWriter(new FileWriter(chatlogs, true));
output.append("[" + time + "] " + line + "\n");
output.close();
}
else if(clients.size() == 2 && logsCount == 0){
Writer output;
String time = new SimpleDateFormat("MM-dd-yyyy_HH:mm:ss").format(Calendar.getInstance().getTime());
output = new BufferedWriter(new FileWriter(chatlogs, true));
output.append("[" + time + "] " + line + "\n");
output.close();
logsCount = 1;
}
else if(clients.size() == 2 && logsCount == 1){
logsCount = 0;
}
}
public String getUsername(){
return name;
}
public void run(){
String line;
try{
while(true){
line = input.readLine();
broadcast(name, line);
}
} catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
}