-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatClient.java
245 lines (226 loc) · 7.88 KB
/
ChatClient.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import java.io.DataInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ChatClient{
// The client socket
private static Socket clientSocket = null;
// The output stream
private static ObjectOutputStream os = null;
// The input stream
private static ObjectInputStream is = null;
private static Scanner scan = null;
private static String username = null;
/*
* To start the ChatClient in console mode use one of the following command
* > java ChatClient username
* > java ChatClient username portNumber
* > java ChatClient username portNumber serverAddress
* at the console prompt
* If the portNumber is not specified 5000 is used
* If the serverAddress is not specified "localHost" is used
*/
public static void main(String[] args) {
// The default port.
int portNumber = 5000;
// The default host.
String host = "localhost";
// different case according to the length of the arguments.
switch(args.length)
{
case 3:
// for > javac ChatClient username portNumber serverAddr
host = args[2];
case 2:
// for > javac ChatClient username portNumber
try
{
portNumber = Integer.parseInt(args[1]);
}
catch(Exception e)
{
System.out.println("Invalid port number.");
System.out.println("Usage is: > java Client username [portNumber] [serverAddress]");
return;
}
case 1:
// for > javac ChatClient username
username = args[0];
break;
default:
// if number of arguments are invalid
System.out.println("Usage is: > java Client username [portNumber] [serverAddress]");
return;
}
System.out.printf("Username : %s \t Port Number: %d \t serverAddress: %s", username, portNumber, host);
/*
* Open a socket on a given host and port. Open input and output streams.
*/
try
{
clientSocket = new Socket(host, portNumber);
scan = new Scanner(System.in);
os = new ObjectOutputStream(clientSocket.getOutputStream());
is = new ObjectInputStream(clientSocket.getInputStream());
String msg = "Connection accepted " + clientSocket.getInetAddress() + ":" + clientSocket.getPort();
display(msg);
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host " + host);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to the host "
+ host);
}
/*
* If everything has been initialized then we want to write some data to the
* socket we have opened a connection to on the port portNumber.
*/
if (clientSocket != null && os != null && is != null) {
try
{
start();//reads and prints on client side
System.out.println("\nHello.! Welcome to the chatroom.");
System.out.println("Instructions:");
System.out.println("1. Simply type the message to send broadcast to all active clients");
System.out.println("2. Type '@username<space>yourmessage' to send message to desired client");
System.out.println("3. Type 'GETUSERS' without quotes to see list of active clients");
System.out.println("4. Type 'SIGNOUT' without quotes to logoff from server");
while(true) {
System.out.print("> ");
// read message from user
String msg = scan.nextLine();
// logout if message is SIGNOUT
if(msg.equalsIgnoreCase("SIGNOUT"))
{
sendMessage(new Message(MessageType.SIGNOUT, ""));
Thread.sleep(2000);
break;
}
// message to check who are present in chatroom
else if(msg.equalsIgnoreCase("GETUSERS"))
{
sendMessage(new Message(MessageType.GETUSERS, ""));
}
// regular text message
else
{
sendMessage(new Message(MessageType.MESSAGE, msg));
}
}
/*
* Close the output stream, close the input stream, close the socket.
*/
CloseAll();
}
catch (Exception e)
{
System.err.println("IOException: " + e);
}
}
}
/* To start the chat client
*/
public static void start() {
// Send our username to the server this is the only message that we
// will send as a String. All other messages will be Message objects
try
{
os.writeObject(username);
boolean NameIsNotOk = true;
while(NameIsNotOk)
{
// read the message form the input datastream
String msg = (String) is.readObject();
// print the message
System.out.println(msg);
System.out.print("> ");
if(msg.equals("OK NAME"))
{
NameIsNotOk = false;
username = msg;
}
else
{
// read the new name from user
String newName = scan.nextLine();
os.writeObject(newName);
}
}
}
catch (IOException eIO) {
display("Exception doing login : " + eIO);
CloseAll();
}
catch(ClassNotFoundException e) {
}
// creates the Thread to listen from the server
new MessageListener(is).start();
}
/*
* To send a message to the console
*/
private static void display(String msg) {
System.out.println(msg);
}
/*
* To send a message to the server
*/
static void sendMessage(Message msg) {
try
{
os.writeObject(msg);
}
catch(IOException e)
{
display("Exception writing to server: " + e);
}
}
/*
* When something goes wrong
* Close the Input/Output streams and disconnect
*/
static private void CloseAll() {
try
{
if(is != null) is.close();
if(os != null) os.close();
if(scan != null) scan.close();
if(clientSocket != null) clientSocket.close();
}
catch(Exception e) {}
}
}
/*
* a class that waits for the message from the server
*/
class MessageListener extends Thread {
private ObjectInputStream is;
MessageListener(ObjectInputStream is)
{
this.is = is;
}
public void run() {
while(true) {
try {
// read the message form the input datastream
String msg = (String) is.readObject();
// print the message
System.out.println(msg);
System.out.print("> ");
}
catch(IOException IOE) {
System.out.println(" *** " + "Server has closed the connection: " + IOE + " *** ");
break;
}
catch(ClassNotFoundException e) {
}
}
}
}