-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
150 lines (118 loc) · 3.67 KB
/
ChatClient.java
File metadata and controls
150 lines (118 loc) · 3.67 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
package clientServer;
import java.net.*;
import java.io.*;
import java.applet.*;
import java.awt.*;
public class ChatClient extends Applet
{
private Socket socket = null;
private DataInputStream console = null;
private DataOutputStream streamOut = null;
private ChatClientThread client = null;
private TextArea display = new TextArea();
private TextField input = new TextField();
private Button send = new Button("Send"), connect = new Button("Connect");
private Button quit = new Button("Bye");
private String serverName = "localhost";
private int serverPort = 4444;
public void init()
{ Panel keys = new Panel();
keys.setLayout(new GridLayout(1,2));
keys.add(quit);
keys.add(connect);
Panel south = new Panel();
south.setLayout(new BorderLayout());
south.add("West", keys);
south.add("Center", input);
south.add("East", send);
Label title = new Label("Simple Chat Client Applet", Label.CENTER);
title.setFont(new Font("Helvetica", Font.BOLD, 14));
setLayout(new BorderLayout());
add("North", title);
add("Center", display);
add("South", south);
quit.setEnabled(false);
send.setEnabled(false);
//getParameters();
}
public boolean action(Event e, Object o)
{
if (e.target == quit)
{ input.setText(".bye");
send();
quit.setEnabled(false);
send.setEnabled(false);
connect.setEnabled(true);
}
else if (e.target == connect) { connect(serverName, serverPort); }
else if (e.target == send)
{ send();
input.requestFocus();
}
return true;
}
public void connect(String serverName, int serverPort)
{
println("Establishing connection. Please wait ...");
try
{ socket = new Socket(serverName, serverPort);
println("Connected: " + socket);
open();
send.setEnabled(true);
connect.setEnabled(false);
quit.setEnabled(true);
}
catch(UnknownHostException uhe)
{ println("Host unknown: " + uhe.getMessage()); }
catch(IOException ioe)
{ println("Unexpected exception: " + ioe.getMessage()); }
}
private void send()
{ try
{ streamOut.writeUTF(input.getText());
streamOut.flush();
input.setText("");
}
catch(IOException ioe)
{ println("Sending error: " + ioe.getMessage());
close();
}
}
public void handle(String msg)
{
if (msg.equals(".bye"))
{ println("Good bye. Press RETURN to exit ...");
close();
}
else println(msg);
}
public void open()
{
try
{ streamOut = new DataOutputStream(socket.getOutputStream());
client = new ChatClientThread(this, socket);
}
catch(IOException ioe)
{ println("Error opening output stream: " + ioe); }
}
public void close()
{
try
{ if (streamOut != null) streamOut.close();
if (socket != null) socket.close();
}
catch(IOException ioe)
{ println("Error closing ..."); }
client.close();
client.stop();
}
private void println(String msg)
{
display.append(msg + "\n");
}
public void getParameters()
{
serverName = getParameter("host");
serverPort = Integer.parseInt(getParameter("port"));
}
}