-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatClient.java
133 lines (114 loc) · 3.39 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
import java.io.*;
import java.nio.*;
import java.nio.channels.SocketChannel;
import java.net.*;
import java.lang.Runnable;
import java.util.*;
/*
ChatClient.java
HoosierChat client program
Written by: Yonjae Lee (yonjlee@indiana.edu)
*/
public class ChatClient {
String serverAddr;
int portNo = 0;
SocketChannel client;
boolean conn = false;
boolean loggedout = false;
public ChatClient() {
}
public static void main(String[] args) {
/* ChatClient [ServerAddress] [ServerPort] */
if (args[0].equals("--help")) {
System.out.println("Usage:");
System.out.println("\tChatClient [ServerAddr] [ServerPort]");
System.out.println("\t\t[ServerAddr] -- IPv4 address of the server");
System.out.println("\t\t[ServerPort] -- Port number of the server");
}
else {
try {
String serverAddr = args[0]; // [ServerAddress]
int portNo = Integer.parseInt(args[1]); // [ServerPort]
new ChatClient().start(serverAddr, portNo);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class SendClient implements Runnable {
public void run() {
Scanner scan = new Scanner(System.in);
String next;
while (conn) {
if (client.isConnected()) {
next = scan.nextLine();
if (next.length() >= 4096) {
System.out.println("ERROR: The input is too long.");
} else {
try {
if (next.equalsIgnoreCase("LOGOUT")) {
loggedout = true;
}
byte [] message = next.getBytes();
ByteBuffer buf = ByteBuffer.wrap(message);
client.write(buf);
buf.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
System.out.println("ERROR: You are not connected to the server.");
conn = false;
}
}
}
}
class RecvClient implements Runnable {
public void run() {
while (conn) {
if (client.isConnected()) {
try {
ByteBuffer buffer = ByteBuffer.allocate(4096);
int numread = -1;
numread = client.read(buffer);
if (numread == -1) {
if (loggedout) {
System.out.println("Successfully logged out; connection closed.");
} else {
System.out.println("Connection closed by server");
}
return;
}
byte[] data = new byte[numread];
System.arraycopy(buffer.array(), 0, data, 0, numread);
System.out.println(new String(data));
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("ERROR: You are not connected to the server.");
conn = false;
}
}
}
}
public void start(String serverAddr, int portNo) throws IOException, InterruptedException {
System.out.println("HoosierChat Client by Yonjae Lee");
System.out.println("Trying to connect to the server " + serverAddr + ":" + Integer.toString(portNo) + "...");
InetSocketAddress server = new InetSocketAddress(serverAddr, portNo);
client = SocketChannel.open(server);
if (client.isConnected()) {
conn = true;
System.out.println("Connected to HoosierChat server. Please register or login.");
System.out.println("Example: REGISTER [username] [password] / LOGIN [username] [password]");
System.out.println("If you need help, enter HELP");
new Thread(new SendClient()).start();
new Thread(new RecvClient()).start();
} else {
System.out.println("Connection Unsuccessful. Please try again.");
}
}
}