-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
273 lines (245 loc) · 7.2 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package heaven.nchat.client;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.ArrayList;
import javax.swing.*;
public class Client extends JFrame {
public static ArrayList<String> usersarr = new ArrayList<String>();
public static ArrayList<String> msgs = new ArrayList<String>();
/**
*
*/
private static final long serialVersionUID = 5625186296847650892L;
JPanel mainPanel = new JPanel();
JTextField msgField = new JTextField();
JButton submit = new JButton("Submit");
public static JList<Object> userList;
public static JList<Object> msgList;
Socket connectSocket;
OutputStream outs;
BufferedOutputStream out;
DataOutputStream put;
InputStream ins;
DataInputStream in;
String username = "user";
String currentHost = "";
boolean connected = false;
public Client() {
setTitle("nchat-client");
setSize(600, 400);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(mainPanel);
userList = new JList<Object>(usersarr.toArray());
JScrollPane scrollPaneUser = new JScrollPane(userList);
scrollPaneUser.setMinimumSize(new Dimension(150, 340));
scrollPaneUser.setPreferredSize(new Dimension(150, 340));
scrollPaneUser.setMaximumSize(new Dimension(151, 340));
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.LINE_AXIS));
msgList = new JList<Object>(msgs.toArray());
JScrollPane scrollPane = new JScrollPane(msgList);
scrollPane.setMinimumSize(new Dimension(450, 400));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
scrollPanel.add(scrollPaneUser, BorderLayout.WEST);
scrollPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(scrollPanel, BorderLayout.CENTER);
JPanel textPanel = new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.LINE_AXIS));
textPanel.setMaximumSize(new Dimension(500, 30));
msgField.setPreferredSize(new Dimension(0, 30));
submit.setPreferredSize(new Dimension(100, 30));
textPanel.add(msgField);
textPanel.add(submit);
mainPanel.add(textPanel, BorderLayout.SOUTH);
msgField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
postMessage();
}
}
});
submit.addActionListener(action);
}
private Action action = new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit)
{
postMessage();
}
}
};
public void addToMsgList(String text)
{
msgs.add(text);
msgList.setListData((Object[])msgs.toArray());
}
public void connect(String host, String nickname)
{
addToMsgList("Connecting to "+host);
try {
connectSocket = new Socket(host, 14668);
outs = connectSocket.getOutputStream();
out = new BufferedOutputStream(outs);
put = new DataOutputStream(out);
ins = connectSocket.getInputStream();
in = new DataInputStream(ins);
put.writeByte(0x02);
put.writeByte(1);
put.writeShort(nickname.length());
put.writeChars(nickname);
put.flush();
currentHost = host;
connected = true;
new Thread () {
boolean noErrors = true;
public void run() {
int packetId = 0;
while(noErrors)
{
try {
if (ins.available() > 0)
{
packetId = ins.read();
switch(packetId)
{
case 0x01:
break;
case 0x04:
short msgLenght = in.readShort();
String msg2 = "";
for (int i = msgLenght; i > 0; i--)
{
msg2 += in.readChar();
}
final String msg = msg2;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
addToMsgList(msg);
}
});
break;
case 0x05:
short msgLenght2 = in.readShort();
String msg3 = "";
for (int i = msgLenght2; i > 0; i--)
{
msg3 += in.readChar();
}
String[] users = msg3.split("\0");
for (String user : users)
{
usersarr.remove(user);
usersarr.add(user);
}
userList.setListData((Object[])usersarr.toArray());
break;
case 0x07:
short msgLenght7 = in.readShort();
String msg27 = "";
for (int i = msgLenght7; i > 0; i--)
{
msg27 += in.readChar();
}
usersarr.remove(msg27);
userList.setListData((Object[])usersarr.toArray());
break;
case 0x06:
short msgLenght6 = in.readShort();
String msg26 = "";
for (int i = msgLenght6; i > 0; i--)
{
msg26 += in.readChar();
}
usersarr.remove(msg26);
usersarr.add(msg26);
userList.setListData((Object[])usersarr.toArray());
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
} catch (Exception e) {
// TODO Auto-generated catch block
addToMsgList("Cannot connect to "+host);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Client ex = new Client();
ex.setVisible(true);
}
});
}
public void postMessage()
{
String msg = msgField.getText();
if (msg != null)
{
if (msg.startsWith("/connect"))
{
try {
put.close();
in.close();
ins.close();
out.close();
outs.close();
connectSocket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
}
if (connected)
{
addToMsgList("Disconnected from "+currentHost);
connected=false;
currentHost="";
}
connect(msg.replace("/connect ", ""), username);
} else if (msg.startsWith("/nick"))
{
username = msg.replace("/nick ", "");
addToMsgList("Nickname changed to: "+username);
} else {
try {
if (connected)
{
put.write((byte) 0x03);
put.writeShort(msg.length());
put.writeChars(msg);
put.flush();
} else {
addToMsgList("Not connected!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
msgField.setText("");
}
}
}