-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathChatClient.java
More file actions
188 lines (149 loc) · 4.89 KB
/
ChatClient.java
File metadata and controls
188 lines (149 loc) · 4.89 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
/**
* Class for chat client.
*/
public class ChatClient {
JTextArea incoming;
JTextField outgoing;
JTextArea userList;
BufferedReader reader;
PrintWriter writer;
Socket sock;
String username;
String[] ps;
/**
* main Function.
*
* @param args The arguments
*/
public static void main(String[] args) {
ChatClient client = new ChatClient();
client.go();
}
/**
* go function.
*/
public void go() {
JFrame frame = new JFrame("Client");
JPanel mainPanel = new JPanel();
JLabel l1, l2;
l1 = new JLabel(Constants.MESSAGE_BOX);
l2 = new JLabel(Constants.USER_LIST);
incoming = new JTextArea(15, 50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
userList = new JTextArea(15, 10);
userList.setLineWrap(true);
userList.setWrapStyleWord(true);
userList.setEditable(false);
JScrollPane uScroller = new JScrollPane(userList);
uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
outgoing = new JTextField(20);
JButton sendButton = new JButton(Constants.SEND);
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(uScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();
username = JOptionPane.showInputDialog(Constants.WELCOME_MESSAGE);
frame.setTitle(username.toUpperCase());
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* function for setting up Networking.
*/
private void setUpNetworking() {
try {
sock = new Socket(Constants.HOST, Constants.PORT);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Networking established");
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Class for send button listener.
* implementing the ActionListener
*/
public class SendButtonListener implements ActionListener {
/**
* function for actionPerformed.
*
* @param ev is an Object of type ActionEvent.
*/
public void actionPerformed(ActionEvent ev) {
try {
writer.println(username + " : " + outgoing.getText());
writer.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
/**
* Class for incoming reader.
*/
public class IncomingReader implements Runnable {
/**
* function for run.
*/
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
if (!messageOrList(message)) {
incoming.append(message + "\n");
} else {
userList.setText("");
for (int i = 1; i < ps.length; i++) {
userList.append(ps[i] + "\n");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* function for messageOrList
*
* @param ms of type ms
*
* @return true or false.
*/
public boolean messageOrList(String ms) {
ps = ms.split(Constants.REG_EX_ESC_PATTERN);
return Constants.SHOW_MSG_VAL_CON_VAL.equals(ps[0]);
}
}
}