-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient2.java
More file actions
144 lines (109 loc) · 4.24 KB
/
ChatClient2.java
File metadata and controls
144 lines (109 loc) · 4.24 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
/* [ChatClient.java]
* A not-so-pretty implementation of a basic chat client
* @author Mangat
* @ version 1.0a
*/
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ChatClient2 {
private JButton sendButton, clearButton;
private JTextField typeField;
private JTextArea msgArea;
private JPanel southPanel;
private Socket mySocket; //socket for connection
private BufferedReader input; //reader for network stream
private PrintWriter output; //printwriter for network output
private boolean running = true; //thread status via boolean
public static void main(String[] args) {
new ChatClient2().go();
}
public void go() {
JFrame window = new JFrame("Chat Client2");
southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2,0));
sendButton = new JButton("SEND");
sendButton.addActionListener(new SendButtonListener());
clearButton = new JButton("QUIT");
clearButton.addActionListener(new QuitButtonListener());
JLabel errorLabel = new JLabel("");
typeField = new JTextField(10);
msgArea = new JTextArea();
southPanel.add(typeField);
southPanel.add(sendButton);
southPanel.add(errorLabel);
southPanel.add(clearButton);
window.add(BorderLayout.CENTER,msgArea);
window.add(BorderLayout.SOUTH,southPanel);
window.setSize(400,400);
window.setVisible(true);
// call a method that connects to the server
connect("192.168.2.21",5000);
// after connecting loop and keep appending[.append()] to the JTextArea
readMessagesFromServer();
}
//Attempts to connect to the server and creates the socket and streams
public Socket connect(String ip, int port) {
System.out.println("Attempting to make a connection..");
try {
mySocket = new Socket("192.168.2.21",5000); //attempt socket connection (local address). This will wait until a connection is made
InputStreamReader stream1 = new InputStreamReader(mySocket.getInputStream()); //Stream for network input
input = new BufferedReader(stream1);
output = new PrintWriter(mySocket.getOutputStream()); //assign printwriter to network stream
//** */
try{ //displays the input from the server to the console
System.out.println(input.readLine());
}catch(NullPointerException npe){
npe.printStackTrace();
}
} catch (IOException e) { //connection error occured
System.out.println("Connection to Server Failed");
e.printStackTrace();
}
System.out.println("Connection made.");
return mySocket;
}
//Starts a loop waiting for server input and then displays it on the textArea
public void readMessagesFromServer() {
while(running) { // loop unit a message is received
try {
if (input.ready()) { //check for an incoming messge
String msg;
msg = input.readLine(); //read the message
System.out.println("received: "+msg);
msgArea.append(msg+"\n");
}
}catch (IOException e) {
System.out.println("Failed to receive msg from the server");
e.printStackTrace();
}
}
try { //after leaving the main loop we need to close all the sockets
input.close();
output.close();
mySocket.close();
}catch (Exception e) {
System.out.println("Failed to close socket");
}
}
//****** Inner Classes for Action Listeners ****
// send - send msg to server (also flush), then clear the JTextField
class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
//Send a message to the client
output.println(typeField.getText());
output.flush();
typeField.setText("");
}
}
// QuitButtonListener - Quit the program
class QuitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
running=false;
}
}
}