-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleChatClientA.java
127 lines (113 loc) · 4.51 KB
/
SimpleChatClientA.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
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static java.nio.charset.StandardCharsets.UTF_8;
public class SimpleChatClientA {
private JTextArea incoming;
private JTextField outgoing;
private PrintWriter writer;
private BufferedReader reader;
public void go(){
/**
* set ups the network connection with local server
* creates GUI display initializes the outing JTextField
* adds and actionlistener to the send button
**/
// call the setupNetworking() method
setUpNetworking();
// make gui and register a listener with the send button
JFrame frame = new JFrame("Simple Chat Client");
JPanel contents = new JPanel();
JScrollPane scroller = createScrollableTextArea();
contents.add(scroller);
outgoing = new JTextField(20);
contents.add(outgoing);
JButton send = new JButton("Send");
send.addActionListener(e->sendMessage());
contents.add(send);
// create a new job an inner class which is a runnable is to read from the server's socket
// stream displaying any incoming messages
// we start this job using a single thread executor since we know we want to run only this one
// job
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new IncomingReader());
frame.getContentPane().add(contents);
frame.setVisible(true);
frame.setSize(400, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JScrollPane createScrollableTextArea() {
/**
* separate method to create the incoming text area from the server
* */
incoming = new JTextArea(15,30);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane scroller = new JScrollPane(incoming);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
return scroller;
}
private void setUpNetworking() {
/**
* knowing the IP address and port number to set up InetSocketAddress
* creates a SocketChannel to open a connection
* Sets ups a bufferedReader to read from the server
* and PrintWriter to send information to the server
* */
// open a SocketChannel to the server
try{
InetSocketAddress serverAdd = new InetSocketAddress("127.0.0.1", 5000);
SocketChannel socketChannel = SocketChannel.open(serverAdd);
reader = new BufferedReader(Channels.newReader(socketChannel, UTF_8));
writer = new PrintWriter(Channels.newWriter(socketChannel,UTF_8));
System.out.println("Networking established.");
}catch (IOException e){
e.printStackTrace();
}
// make a printwriter and assign to writer instance variable
}
private void sendMessage(){
/***
* gets the text from the outgoing JTextField
* and prints it to the server using the println()
* flushes the PrintWriter and prepares to send the next message
*/
/
// get the text from the text field and
writer.println(outgoing.getText());
writer.flush();
outgoing.setText("");
outgoing.requestFocus();
// send it to the server using the writer (a PrintWriter)
}
public class IncomingReader implements Runnable{
/***
* runnable used by the thread in the go() method
* checks if there is any input from the server
* if there is it stores in message and appends() the
* message to the incoming window that displays to the user
*/
public void run() {
String message;
try{
while((message = reader.readLine())!=null){
System.out.println("read " +message);
incoming.append(message+"\n");
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public static void main(String[] args){
new SimpleChatClientA().go();
}
}