This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppletClient.java
More file actions
66 lines (52 loc) · 1.64 KB
/
AppletClient.java
File metadata and controls
66 lines (52 loc) · 1.64 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
import java.io.*;
import java.net.*;
import javax.swing.*;
public class AppletClient extends JApplet {
// Label for displaying the visit count
private JLabel jlblCount = new JLabel();
// Indicate if it runs as application
private boolean isStandAlone = false;
// Host name or ip
private String host = "localhost";
/** Initialize the applet */
public void init() {
add(jlblCount);
try {
// Create a socket to connect to the server
Socket socket;
if (isStandAlone)
socket = new Socket(host, 8000);
else
socket = new Socket(getCodeBase().getHost(), 8000);
// Create an input stream to receive data from the server
DataInputStream inputFromServer =
new DataInputStream(socket.getInputStream());
// Receive the count from the server and display it on label
int count = inputFromServer.readInt();
jlblCount.setText("You are visitor number " + count);
// Close the stream
inputFromServer.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
/** Run the applet as an application */
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Applet Client");
// Create an instance of the applet
AppletClient applet = new AppletClient();
applet.isStandAlone = true;
// Get host
if (args.length == 1) applet.host = args[0];
// Add the applet instance to the frame
frame.add(applet, java.awt.BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.setVisible(true);
}
}