-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientGUI.java
More file actions
343 lines (315 loc) · 14.5 KB
/
Copy pathClientGUI.java
File metadata and controls
343 lines (315 loc) · 14.5 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ILLUMINATI
*/
class chatClient implements Runnable{
Socket SOCK;
ClientGUI GUI;
PrintWriter OUT;
Scanner sc;
chatClient(Socket SOCK, ClientGUI GUI) throws IOException{
this.SOCK = SOCK;
this.GUI = GUI;
}
@Override
public void run() {
try {
OUT = new PrintWriter(SOCK.getOutputStream());
sc = new Scanner(SOCK.getInputStream());
} catch (IOException ex) {
Logger.getLogger(chatClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Cannot set input and output streams");
}
OUT.println(GUI.getTextthname());
OUT.println(GUI.getTextthname());
OUT.println("Welcome");
OUT.flush();
String inp;
while(true){
if(sc.hasNext()){
inp = sc.nextLine();
System.out.println(inp);
GUI.setTextchat(inp + "\n");
}
}
}
public void sendMsg(String msg){
OUT.println(msg);
OUT.flush();
}
}
public class ClientGUI extends javax.swing.JFrame {
chatClient Client;
boolean isConnected;
int PORT;
String HOST;
Socket SOCK;
String username;
/**
* Creates new form ClientGUI
*/
public ClientGUI() {
initComponents();
// sendB.setEnabled(false);
sendB.setEnabled(false);
Nametf.setEnabled(false);
message.setEnabled(false);
chat.setEditable(false);
}
public String getTextthname(){
return thname.getText();
}
public void setTextchat(String msg){
chat.append(msg);
}
public void Connect() throws IOException{
SOCK = new Socket (HOST,PORT);
isConnected = true;
Client = new chatClient(SOCK,this);
Thread tClient = new Thread(Client);
tClient.start();
String msg = "You are connected to : " + HOST + ":" + PORT;
System.out.println(msg);
chat.append( msg + "\n");
}
public boolean checkConnected(){
if(SOCK.isConnected()){
return true;
} else {
isConnected = false;
return false;
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
Nametf = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
Hosttf = new javax.swing.JTextField();
Porttf = new javax.swing.JTextField();
sendB = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
chat = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
logoutB = new javax.swing.JButton();
clearB = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
thname = new javax.swing.JTextField();
connect = new javax.swing.JButton();
message = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name :");
jLabel2.setText("Message:");
jLabel3.setText("Server:");
jLabel4.setText("Port:");
Hosttf.setText("localhost");
Porttf.setText("1234");
sendB.setText("Send");
sendB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendBActionPerformed(evt);
}
});
chat.setColumns(20);
chat.setRows(5);
jScrollPane2.setViewportView(chat);
jLabel5.setText("Chat History");
logoutB.setText("Disconnect");
clearB.setText("Clear");
clearB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearBActionPerformed(evt);
}
});
jLabel6.setText("Name:");
connect.setText("Connect");
connect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
connectActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(30, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 694, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(Hosttf, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Porttf, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(thname, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(connect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(clearB)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(logoutB))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Nametf, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendB, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(Hosttf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(Porttf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(logoutB)
.addComponent(clearB)
.addComponent(jLabel6)
.addComponent(thname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(connect))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Nametf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sendB))
.addGap(50, 50, 50))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void sendBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendBActionPerformed
if(!checkConnected()){
chat.append("Connection Lost!! :(\n");
sendB.setEnabled(false);
Nametf.setEnabled(false);
message.setEnabled(false);
} else {
String msg = Nametf.getText() + "\n" + message.getText();
Client.sendMsg(msg);
chat.append("To " + Nametf.getText() + " : " + message.getText() + "\n");
message.setText("");
message.requestFocus();
}
}//GEN-LAST:event_sendBActionPerformed
private void connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectActionPerformed
if(!isConnected){
HOST = Hosttf.getText();
String temp_name = thname.getText();
if(temp_name == null || temp_name.equals("")){
chat.append("Improper username\n");
System.out.println("Inavalid username");
return;
}
try{
PORT = Integer.parseInt(Porttf.getText());
Connect();
sendB.setEnabled(true);
Nametf.setEnabled(true);
message.setEnabled(true);
this.getRootPane().setDefaultButton(sendB);
Nametf.requestFocus();
} catch (NumberFormatException e){
System.out.println("Invalid port no.");
chat.append("Improper port " + Porttf.getText() + "\n");
Porttf.requestFocus();
return;
} catch (IOException ex) {
System.out.println(ex);
chat.append("Not found " +HOST + ":" + PORT + "\n");
return;
}
}
}//GEN-LAST:event_connectActionPerformed
private void clearBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearBActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_clearBActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClientGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField Hosttf;
private javax.swing.JTextField Nametf;
private javax.swing.JTextField Porttf;
private javax.swing.JTextArea chat;
private javax.swing.JButton clearB;
private javax.swing.JButton connect;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JButton logoutB;
private javax.swing.JTextField message;
private javax.swing.JButton sendB;
private javax.swing.JTextField thname;
// End of variables declaration//GEN-END:variables
}