31.4 (Count clients) Write a server that tracks the number of the clients connected to the server. When a new connection is established, the count is incremented by 1. The count is stored using a random-access file. Write a client program that receives the count from the server and display a message, such as You are visitor number 11, as shown in Figure 31.19. Name the client Exercise31_04Client and the server Exercise31_04Server.
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Exercise04s extends JFrame{ private static final long serialVersionUID = 1L; private int ClientNumber = 0; private JTextArea jta = new JTextArea(); private JButton jButton = new JButton("Stop"); private int count; private RandomAccessFile raf; public static void main(String[] args) { new Exercise04s(); } public Exercise04s() { jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { raf.seek(0); raf.writeInt(count); System.exit(0); } catch (IOException e2) { e2.printStackTrace(); } } }); setLayout(new BorderLayout()); add(new JScrollPane(jta), BorderLayout.CENTER); add(jButton, BorderLayout.SOUTH); jta.append("Server started at count: " + count + "\n\n"); setTitle("Exercise04s"); setSize(500, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); try { // Create or open the count file raf = new RandomAccessFile("count.dat", "rw"); // Get the count if (raf.length() == 0) count = 0; else count = raf.readInt(); // Create a server socket @SuppressWarnings("resource") ServerSocket serverSocket = new ServerSocket(8000); while (true) { Socket socket = serverSocket.accept(); jta.append("Starting thread " + ClientNumber + "\n"); ClientNumber++; InetAddress inetAddress = socket.getInetAddress(); jta.append("Client IP " + inetAddress.getHostAddress() + "\n\n"); HandleAClient task = new HandleAClient(socket); new Thread(task).start(); } } catch (IOException e) { e.printStackTrace(); } } class HandleAClient implements Runnable { private Socket socket; public HandleAClient(Socket socket) { this.socket = socket; } @Override public void run() { try { count++; DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); outputToClient.writeInt(count); } catch (IOException e) { e.printStackTrace(); } } } }
import java.io.*; import java.net.*; import javax.swing.*; public class Exercise04c extends JApplet { private static final long serialVersionUID = 1L; // 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 */ @SuppressWarnings("resource") 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("Exercise04c"); // Create an instance of the applet Exercise04c applet = new Exercise04c(); 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.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
No comments :
Post a Comment