-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipKchat.java
More file actions
778 lines (715 loc) · 26.2 KB
/
Copy pathipKchat.java
File metadata and controls
778 lines (715 loc) · 26.2 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
* Copyright (c) 2000-2001 ipKangaroo
*
*/
/*
This class is the executable for ipKchat GUI version. It sets up
the GUI and procedes to write to the socket when needed. Class
'ipKFrom' is a thread that listenes on the socket and displays
any message in the main dialogue box. Notice also that this class
is inline. There is no need for this, this is a small program with
a small computational load. The authors were a bit lazy and still
thinking in the 'fast' mindset from the 'Server' development.
Adding Functions in the program wouldn't hurt it a bit. It would
make it slightly smaller and easier to read.
*/
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.*;
import java.util.*;
public class ipKchat extends Frame implements ActionListener, KeyListener
{
static Socket s;
static int port = 2222; // default server port
//static String host = "localhost"; // default server host
static String host = "ipkangaroo.no-ip.com"; // default server host
static String id = "";
static TextField tf1, tf2, tf3, tf4, tf5;
static TextArea ta1;
static OutputStream os;
static BufferedReader is;
static byte[] outbytes;
static Button star, Start, Disconnect, Connect, getQ;
static String disp = "";
static int CurCon = 0;
static int connectNow = 0;
static int serverr = 0;
static CheckboxMenuItem cboxM;
static MenuItem cboxQ;
static sKeepAlive keepS;
static Panel panelbot;
static GridBagConstraints gbc;
static int qON = 0;
static String quoteString = null;
static Process talk;
Process ServeiN;
public void QN_Action(ActionEvent event) //Activates 'Quote Now!' service
{
if(qON == 0 && serverr == 0)
{
add(panelbot, gbc);
pack();
show ();
qON = 1;
ta1.append("\n");
ta1.append("Quote Now! Made possible by: Money.com\n");
ta1.append("\n");
}
else
{
if(qON == 1)
{
remove(panelbot);
pack();
show ();
qON = 0;
ta1.append("\n");
ta1.append("Thank you for using Quote Now!\n");
ta1.append("\n");
}
}
}
public void New_Action(ActionEvent event) //Displays 'About' dialogue
{
About a = new About();
}
public void Help_Action(ActionEvent event) //Displays 'Help dialogue
{
helpC hehe = new helpC();
}
public void Quit_Action(ActionEvent event) //Quits client
{
if(serverr == 1) //If in server mode it stops
{
ServeiN.destroy();
}
if(CurCon == 1) //Otherwise kill connection and exit
{
try
{
disp = "%q\n";
ta1.append("(" + id + ":) " + disp + "\n");
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
}
catch(IOException e)
{
ta1.append(e.getMessage() + "\n");
try
{
s.close();
ta1.append("You have disconnected from the server...\n");
}
catch(java.io.IOException r)
{
ta1.setText((r + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
}
}
this.dispose();
System.exit(0);
}
public void Start_SAction(ActionEvent event) //Starts server from GUI
{
if(qON == 1)
{
remove(panelbot);
pack();
show ();
qON = 0;
ta1.append("\n");
ta1.append("Thank you for using Quote Now!\n");
ta1.append("\n");
}
if(CurCon == 0)
{
ta1.append("The Server is Starting...\n");
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(false);
tf1.setEditable(false);
tf3.setEditable(false);
tf4.setEditable(false);
tf4.setText(" <<< *** ipK Server Mode *** >>> ");
try
{
ServeiN = Runtime.getRuntime().exec("java Server");
serverr = 1;
keepS = new sKeepAlive(ServeiN); //Prints Server's log to the main dialogue box
keepS.start();
}
catch (Exception e) {ta1.append(e.getMessage() + "\n");}
}
else
{
ta1.append("Disconnect first to start Server...\n");
}
}
public void LipKchat_Action(ActionEvent event) //Starts another instance of ipKchat
{
try
{
Runtime.getRuntime().exec("java ipKchat");
}
catch (Exception e) {ta1.append(e.getMessage() + "\n");}
}
public void Stop_SAction(ActionEvent event) //Stops the server and returns
{ // the GUI to it's start up state
if(serverr == 1)
{
serverr = 0;
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
tf1.setEditable(true);
tf3.setEditable(true);
tf4.setEditable(true);
tf4.setText("");
ServeiN.destroy();
ta1.append("The Server has stoped...\n");
}
else
{
ta1.append("You must start the Server before you can stop it...\n");
}
}
public void LipKtalk_Action(ActionEvent event) //Executes ipKchat
{
try
{
talk = Runtime.getRuntime().exec("java ipKtalk");
}
catch (Exception e) {}
}
public void keyReleased(KeyEvent key)
{
//System.out.println("Key Released: " +
//e.getKeyText(e.getKeyCode()));
}
public void keyPressed(KeyEvent key)
{
//System.out.println("Key Pressed: "
//+ e.getKeyText(e.getKeyCode()));
}
public void keyTyped(KeyEvent key) //If enter is pressed it send the text in the text field
{
if('\n' == key.getKeyChar())
{
if(CurCon == 1)
{
try
{
disp = tf4.getText();
if(disp.equals("%q"))
{
try
{
disp = "%q\n";
tf4.setText("");
ta1.append("(" + id + ":) " + disp + "\n");
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
s.close();
ta1.setText("You have disconnected from the server...\n");
}
catch(java.io.IOException e)
{
ta1.setText((e + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
else
{
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
tf4.setText("");
}
}
catch (IOException e)
{
ta1.append(e.getMessage() + "\n");
try
{
s.close();
ta1.append("You have disconnected from the server...\n");
}
catch(java.io.IOException r)
{
ta1.setText((r + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
}
}
}
}
class ActionListener1 implements ActionListener //Listenes for menu item actions
{
public void actionPerformed(ActionEvent event)
{
String str = event.getActionCommand();
if (str.equals("About"))
New_Action(event);
else if (str.equals("Help"))
Help_Action(event);
else if (str.equals("Exit"))
Quit_Action(event);
else if (str.equals("Start Server"))
Start_SAction(event);
else if (str.equals("Stop Server"))
Stop_SAction(event);
else if (str.equals("Launch ipKtalk"))
LipKtalk_Action(event);
else if (str.equals("ipKchat"))
LipKchat_Action(event);
else if (str.equals("Quote Now!"))
QN_Action(event);
}
}
public ipKchat() //Sets up the GUI
{
Image icon = getToolkit().getImage("chat.gif");
this.setIconImage(icon);
setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
Panel panelbut = new Panel();
Panel paneltxtfld = new Panel();
Panel panelmid = new Panel();
Panel paneltext = new Panel();
panelbot = new Panel();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(paneltxtfld, gbc);
add(panelmid, gbc);
add(panelbut, gbc);
add(paneltext, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridwidth = 1;
Label l1 = new Label("IP Address:", Label.RIGHT);
paneltxtfld.add(l1);
tf1 = new TextField(20);
paneltxtfld.add(tf1, gbc);
Label l2 = new Label("", Label.RIGHT);
paneltxtfld.add(l2);
Label l3 = new Label("", Label.RIGHT);
paneltxtfld.add(l3);
Connect = new Button ("Connect");
paneltxtfld.add(Connect);
Connect.addActionListener(this);
Disconnect = new Button ("Disconnect");
paneltxtfld.add(Disconnect);
Disconnect.addActionListener(this);
Disconnect.setEnabled(false);
Label l4 = new Label("My IP: ", Label.RIGHT);
panelmid.add(l4);
tf2 = new TextField(16);
panelmid.add(tf2, gbc);
tf2.setEditable(false);
star = new Button("*");
star.setEnabled(false);
panelmid.add(star);
Label l5 = new Label("", Label.RIGHT);
panelmid.add(l5);
Label l6 = new Label("My Name:", Label.RIGHT);
panelmid.add(l6);
tf3 = new TextField(16);
panelmid.add(tf3, gbc);
tf4 = new TextField(52);
panelbut.add(tf4, gbc);
Start = new Button("Send");
Font font = new Font ("Times", Font.BOLD, 14);
Color numberColor = new Color (46, 124, 191);
setFont (font);
panelbut.add(Start);
Start.setEnabled(false);
Start.setBackground(numberColor);
star.setBackground(numberColor);
Start.addActionListener(this);
star.addActionListener(this);
ta1 = new TextArea("Welcome to ipKchat!\nBrought to you by, ipKangaroo...\n\n", 20, 60,1);
gbc.gridwidth = GridBagConstraints.REMAINDER;
paneltext.add(ta1, gbc);
ta1.setEditable(false);
Label l9 = new Label("Ticker:", Label.RIGHT);
panelbot.add(l9);
tf5 = new TextField(40);
panelbot.add(tf5, gbc);
getQ = new Button ("Get Quote");
panelbot.add(getQ);
getQ.addActionListener(this);
setTitle ("ipKchat");
setBackground(Color.white);
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu(" File ");
Menu server = new Menu(" Server ");
Menu options = new Menu(" Options ");
Menu ipK = new Menu(" ipK ");
ipK.add(new MenuItem("-"));
ipK.add(new MenuItem("Launch ipKtalk"));
ipK.add(new MenuItem("-"));
cboxM = new CheckboxMenuItem("Audio Alert");
options.add(cboxM);
cboxQ = new MenuItem("Quote Now!");
options.add(cboxQ);
server.add(new MenuItem("Start Server"));
server.add(new MenuItem("Stop Server"));
server.add(new MenuItem("-"));
server.add(new MenuItem("ipKchat"));
fileMenu.add(new MenuItem("About"));
fileMenu.add(new MenuItem("Help"));
fileMenu.add(new MenuItem("-"));
fileMenu.add(new MenuItem("Exit"));
menuBar.add(fileMenu);
menuBar.add(options);
menuBar.add(server);
menuBar.add(ipK);
setMenuBar (menuBar);
WindowListener1 lWindow = new WindowListener1();
addWindowListener(lWindow);
ActionListener1 lAction = new ActionListener1();
fileMenu.addActionListener(lAction);
options.addActionListener(lAction);
server.addActionListener(lAction);
ipK.addActionListener(lAction);
tf4.addKeyListener(this);
cboxM.setState(false);
this.setLocation(160,210);
setResizable(false);
pack();
show ();
pack();
show ();
}
public void actionPerformed(ActionEvent event) //Listenes for button events
{
if(event.getActionCommand() == "*") //Sends the ip address of the current client
{
try
{
disp = tf2.getText();
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
}
catch(IOException e)
{
ta1.append(e.getMessage() + "\n");
try
{
s.close();
ta1.append("You have disconnected from the server...\n");
}
catch(java.io.IOException r)
{
ta1.setText((r + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
}
}
if(event.getActionCommand() == "Send") //Sends the text in the text input area, same as enter
{
try
{
disp = tf4.getText();
if(disp.equals("%q"))
{
try
{
disp = "%q\n";
tf4.setText("");
ta1.append("(" + id + ":) " + disp + "\n");
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
s.close();
ta1.setText("You have disconnected from the server...\n");
}
catch(java.io.IOException e)
{
ta1.setText((e + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
else
{
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
tf4.setText("");
}
}
catch (IOException e)
{
ta1.append(e.getMessage() + "\n");
try
{
s.close();
ta1.append("You have disconnected from the server...\n");
}
catch(java.io.IOException r)
{
ta1.setText((r + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
}
}
if(event.getActionCommand() == "Connect") //Initiates a socket connection
{
id = tf3.getText();
if(id.equals(""))
{
ta1.append("\n");
ta1.append("Enter a user name first, under 'My Name:' ...\n");
ta1.append("\n");
}
else
{
ta1.append("\n");
ta1.append("\n");
ta1.append("Your username is: " + id + "\n");
ta1.append("------------------------------------------------------------\n");
ta1.append("Please wait while connecting...");
ta1.append("\n");
host = tf1.getText();
try
{
s = new Socket(host, port);
os = s.getOutputStream();
is = new BufferedReader(new InputStreamReader(s.getInputStream()));
ipKFrom f = new ipKFrom(is);
f.start(); //Starts recieving thread...
outbytes = (id + "\n").getBytes();
os.write(outbytes);
os.flush();
Start.setEnabled(true);
star.setEnabled(true);
Disconnect.setEnabled(true);
Connect.setEnabled(false);
CurCon = 1;
}
catch (UnknownHostException e)
{
ta1.append("unknown host: " + host + "\n");
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
catch (IOException e)
{
ta1.append(e.getMessage() + "\n");
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
}
}
if(event.getActionCommand() == "Disconnect") //Closes the connection
{
try
{
disp = "%q\n";
ta1.append("(" + id + ":) " + disp + "\n");
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
s.close();
ta1.setText("You have disconnected from the server...\n");
}
catch(java.io.IOException e)
{
ta1.setText((e + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
if(event.getActionCommand() == "Get Quote") //Get the quotes using Quote Now
{
quoteString = tf5.getText();
String token = null;
Quote q = new Quote();
ta1.append("\n\n");
StringTokenizer st = new StringTokenizer(quoteString);
while (st.hasMoreTokens())
{
infoQ info = q.getQ(st.nextToken());
ta1.append("Symbol: " + info.symbol + "\n");
ta1.append("Name: " + info.name + "\n");
ta1.append("Price: " + info.price + "\n");
ta1.append("Change: " + info.change + "\n");
ta1.append("Volume: " + info.volume + "\n");
ta1.append("\n");
}
ta1.append("\n");
}
}
class WindowListener1 extends WindowAdapter //Listens for window events, same as quit
{
public void windowClosing(WindowEvent event)
{
if(serverr == 1)
{
ServeiN.destroy();
}
if(CurCon == 1)
{
try
{
disp = "%q\n";
outbytes = (disp + "\n").getBytes();
os.write(outbytes);
os.flush();
}
catch(IOException e)
{
ta1.append(e.getMessage() + "\n");
try
{
s.close();
ta1.append("You have disconnected from the server...\n");
}
catch(java.io.IOException r)
{
ta1.setText((r + "\n"));
}
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
}
}
Window win = event.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}
}
public static void main (String argv[]) //Parses command line arguments and sets up the Application
{
String usage = "usage: java DemoClient [-h host] [-p port] ID " +
"\n (default port=" + port +", host=" + host + ")";
try
{
Arguments arguments = new Arguments(argv); //Start parsing commmand line
if (arguments.isSpecified("h"))
{
host = arguments.getModified("h");
}
if (arguments.isSpecified("p"))
{
port = arguments.getModifiedInt("p");
}
if (arguments.getUnmodified().size() > 0)
{
id = (String) arguments.getUnmodified().get(0); //End parse
connectNow = 1;
}
}
catch (NumberFormatException e)
{
System.err.println("bad number format");
System.exit(1);
}
System.out.println("");
System.out.println("Port: " + port);
System.out.println("Host: " + host);
System.out.println("ID: " + id);
System.out.println("");
ipKchat m = new ipKchat ();
tf3.setText(id);
tf1.setText(host);
InetAddress iNet = null;
try
{
iNet = InetAddress.getLocalHost();
tf2.setText(iNet.toString());
}
catch(java.net.UnknownHostException e)
{
ta1.append("\n" + e + "\n");
}
if(connectNow == 1)
{
host = tf1.getText();
id = tf3.getText();
try
{
s = new Socket(host, port);
os = s.getOutputStream();
is = new BufferedReader(new InputStreamReader(s.getInputStream()));
ipKFrom f = new ipKFrom(is);
f.start(); //Starts recieving thread...
outbytes = (id + "\n").getBytes(); // this client
os.write(outbytes);
os.flush();
ta1.append("\n");
ta1.append("\n");
ta1.append("Your username is: " + id + "\n");
ta1.append("------------------------------------------------------------\n");
ta1.append("\n");
Start.setEnabled(true);
star.setEnabled(true);
Disconnect.setEnabled(true);
Connect.setEnabled(false);
CurCon = 1;
}
catch (UnknownHostException e)
{
ta1.append("unknown host: " + host + "\n");
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
catch (IOException e)
{
ta1.append(e.getMessage() + "\n");
Start.setEnabled(false);
star.setEnabled(false);
Disconnect.setEnabled(false);
Connect.setEnabled(true);
CurCon = 0;
}
}
}
}
//End class ipKchat