-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.java
143 lines (93 loc) · 4.18 KB
/
client.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
import java.io.*;
import java.net.*;
import java.util.*;
class client{
//This is a function that can be used to receive a chatroom correctly
public static void receiveChatroom(BufferedReader br) {
String chatline = "";
//read the first line of the chatroom
try{
chatline = br.readLine();
} catch (Exception e) { System.out.println("oops\n");}
//while we have not reached the end of the chatroom (deliminated by the code 567), loop through each chatline and print it out
while (!chatline.contains("567"))
{
System.out.println(chatline);
try{
chatline = br.readLine();
} catch (IOException e) { System.out.println("oops\n");}
}
}
public static void main(String[] Args) {
Console console = System.console();
try {
boolean loop = true;
Socket s = new Socket("mono", 8080);
InputStreamReader isr;
BufferedReader br;
PrintStream ps;
isr = new InputStreamReader(s.getInputStream());
br = new BufferedReader(isr);
ps = new PrintStream(s.getOutputStream());
while (loop) {
System.out.println("OPTIONS");
System.out.println("1 Create Chatroom"); //create chatroom
System.out.println("2 List Chatrooms"); // list existing chatrooms
System.out.println("3 Join a Chatroom"); // join a chatroom (exit will be contained within this command)
System.out.println("4 Exit");
String command = console.readLine();
// ask for a chatroom name and send it to the server
if (command.equals("1")) {
System.out.println("What is the name of the chatroom you want to create?\n");
String chatroomName = console.readLine();
ps.println("1");
ps.println(chatroomName);
ps.flush();
}
// ask for list of chatrooms from the server, display them
if (command.equals("2")) {
ps.println("2");
ps.flush();
System.out.println(br.readLine());
}
// join a chatroom, continues until the user types exit
if (command.equals("3")) {
System.out.println("What is the name of the chatroom you want to join?\n");
String chatroomName = console.readLine();
System.out.println("What is your name?\n");
String Name = console.readLine();
ps.println("3");
ps.println(Name);
ps.println(chatroomName);
ps.flush();
boolean notExit = true;
String message = "";
while (notExit)
{
//receive the state of the chatroom (contains all the chats deliminated by 567) and print it to console
receiveChatroom(br);
System.out.println("Type a message (or type exit to leave):\n");
//blocks until the user types a message and presses enter
message = console.readLine();
ps.println(message);
if (message.contains("exit"))
{
notExit = false;
}
ps.flush();
message = "";
}
}
if (command.equals("4")) {
ps.println("4");
ps.flush();
loop = false;
}
}
s.close();
} catch (
Exception E) {
System.out.println("oops");
}
}
}