forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
133 lines (124 loc) · 4.13 KB
/
ChatClient.java
File metadata and controls
133 lines (124 loc) · 4.13 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
/**
* Java. Level 2. Lesson 8
* Simple chat client
* To make jar file:
* > create manifest.txt file with 1 line
* Main-Class: ChatClient
* > jar -cvmf manifest.txt ChatClient.jar
* ChatClient*.class IConstants.class
*
* @author Sergey Iryupin
* @version 0.2.4 dated Jun 29, 2018
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
class ChatClient extends JFrame implements ActionListener, IConstants {
final String TITLE_OF_PROGRAM = "Client for net.chat";
final int WINDOW_WIDTH = 350;
final int WINDOW_HEIGHT = 450;
final String BTN_ENTER = "Enter";
final String AUTH_INVITATION = "You must login using command\n"+
"auth <login> <passwd>";
JTextArea dialogue; // area for dialog
JTextField message; // field for entering messages
boolean isAuthorized; // flag of authorization
Socket socket;
PrintWriter writer;
BufferedReader reader;
public static void main(String[] args) {
new ChatClient();
}
/**
* Constructor:
* Creating a window and all the necessary elements on it
*/
ChatClient() {
setTitle(TITLE_OF_PROGRAM);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLocationRelativeTo(null); // to the center
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
try {
writer.println(EXIT_COMMAND);
writer.flush();
} catch (Exception ex) {}
}
});
// area for dialog
dialogue = new JTextArea();
dialogue.setLineWrap(true);
dialogue.setEditable(false);
JScrollPane scrollBar = new JScrollPane(dialogue);
// panel for connamd field and button
JPanel bp = new JPanel();
bp.setLayout(new BoxLayout(bp, BoxLayout.X_AXIS));
message = new JTextField();
message.addActionListener(this);
JButton enter = new JButton(BTN_ENTER);
enter.addActionListener(this);
// adding all elements to the window
bp.add(message);
bp.add(enter);
add(BorderLayout.CENTER, scrollBar);
add(BorderLayout.SOUTH, bp);
setVisible(true);
Connect(); // connect to the Server
}
/**
* Connect to the Server
*/
void Connect() {
dialogue.append(AUTH_INVITATION + "\n");
isAuthorized = false;
try {
socket = new Socket(SERVER_ADDR, SERVER_PORT);
writer = new PrintWriter(socket.getOutputStream());
reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
new Thread(new ServerListener()).start(); // start Server listener
} catch (Exception ex) {
dialogue.append(ex.getMessage() + "\n");
}
}
/**
* ServerListener: get messages from Server
*/
class ServerListener implements Runnable {
String message;
public void run() {
try {
while ((message = reader.readLine()) != null) {
if (message.startsWith("Hello, ")) // check authorisation
isAuthorized = true;
if (!message.equals("\0") && isAuthorized)
dialogue.append(message + "\n");
if (message.equals(AUTH_FAIL))
System.exit(-1); // terminate client
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.exit(-1); // terminate client
}
}
/**
* Listener of events from message field and enter button
*/
@Override
public void actionPerformed(ActionEvent event) {
if (message.getText().trim().length() > 0) {
try {
writer.println(message.getText());
writer.flush();
} catch (Exception ex) {}
}
message.setText("");
message.requestFocusInWindow();
}
}