-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClientThread.java
More file actions
80 lines (56 loc) · 1.88 KB
/
ChatClientThread.java
File metadata and controls
80 lines (56 loc) · 1.88 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
/**
* Created 2/16/2003 by Ting Zhang
* Part of implementation of the ChatClient to receive
* all the messages posted to the chat room.
*/
// socket
import java.net.*;
import java.io.*;
// Swing
import javax.swing.JTextArea;
// Crypto
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.*;
public class ChatClientThread extends Thread {
private ChatClient _client;
private JTextArea _outputArea;
private Socket _socket = null;
public ChatClientThread(ChatClient client) {
super("ChatClientThread");
_client = client;
_socket = client.getSocket();
_outputArea = client.getOutputArea();
}
public void run() {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
_socket.getInputStream()));
String msg;
while ((msg = in.readLine()) != null) {
if (msg.equals(CommunicationType.KEY.toString())){
//Cliente session key yollandi.
String sessionKeyStr = in.readLine();
byte[] decodedKey = Base64.getDecoder().decode(sessionKeyStr);
SecretKey sessionKey = new SecretKeySpec(decodedKey,0,decodedKey.length,"AES");
_client.setSessionKey(sessionKey);
}
/*else if (msg.equals(CommunicationType.SET_ID.toString())){
String clientId = in.readLine();
_client.set_clientId(Integer.parseInt(clientId));
}*/else {
consumeMessage(msg + " \n");
}
}
_socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void consumeMessage(String msg) {
if (msg != null) {
_outputArea.append(msg);
}
}
}