Repository navigation
Expand file tree
/
Copy pathClient.java
More file actions
169 lines (123 loc) · 3.56 KB
/
Copy pathClient.java
File metadata and controls
169 lines (123 loc) · 3.56 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
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
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class Client extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket connection;
private String message = "";
private String serverIP;
// constructor and the GUI
// since we dont want everyone to access our computer
// we want to make sure it just connects to host(so the ip address of the server)
public Client(String host) {
super("Client Side Instant Messenger");
serverIP = host;
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText,BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow),BorderLayout.CENTER);
setSize(700,400);
setVisible(true);
}
// connect to server
public void startRunning() {
try {
// we don't wanna wait for connection like server
connectToServer();
setupStreams();
whileChatting();
} catch (EOFException eofexception) {
showMessage("\nClient terminated connection");
}catch(IOException ioException) {
ioException.printStackTrace();
}finally {
closeStuff();
}
}
private void connectToServer() throws IOException {
showMessage("Attempting Connection...\n");
// we gotta know the port number of the host
connection = new Socket(InetAddress.getByName(serverIP), 6789);
showMessage("Connected to: "+connection.getInetAddress().getHostName());
}
// setup stream to send and receive messages
private void setupStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\nStreams are now setup \n");
}
// while chatting with server
private void whileChatting() throws IOException {
ableToType(true);
do {
try {
message = (String)input.readObject();
showMessage("\n "+ message);
} catch (ClassNotFoundException classNotFoundException) {
// TODO: handle exception
showMessage("\n I don't know that object type");
}
} while (!message.equals("MOZHGAN: END"));
}
// closing the streams and sockets
private void closeStuff() {
showMessage("\nClosing Streams and Sockets down...");
ableToType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException ioException) {
// TODO: handle exception
ioException.printStackTrace();
}
}
// sending the messages to server
private void sendMessage(String msg) {
try {
output.writeObject("MOZHGAN :"+ msg);
output.flush();
showMessage("\nMOZHGAN: "+msg);
} catch (IOException ioException) {
// TODO: handle exception
chatWindow.append("\nCould not send the message successfully! ");
}
}
// change/ update the chat area
private void showMessage(final String m) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
chatWindow.append(m);
}
}
);
}
// gives user permission to type into the textbox
private void ableToType(final boolean tof) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
userText.setEditable(tof);
}
}
);
}
}