Explain How Does The Client Receives The Object And Prints The Date

Explain How Does The Client Receives The Object And Prints The Date


  • 17 Feb, 2012

    The client, DateClient, does not have to send any messages to the DateServer once a connection has been established. It simply receives a Date object that represents the current day and time of the remote machine. The client receives the object and prints the date as shown in Code Sample.Code Sample: DateClient.javaimport java.io.*;import java.net.*;import java.util.*;public class DateClient {public static void main(String argv[]) {ObjectOutputStream oos = null;ObjectInputStream ois = null;Socket socket = null;Date date = null;try {// open a socket connectionsocket = new Socket("yourMachineNameORipAddress", 3000);// open I/O streams for objectsoos = new ObjectOutputStream(socket.getOutputStream());ois = new ObjectInputStream(socket.getInputStream());// read an object from the serverdate = (Date) ois.readObject();System.out.print("The date is: " + date);oos.close();ois.close();} catch(Exception e) {System.out.println(e.getMessage()); } }}To run this example, the first step is to replace the bold line in DateClient with the machine name or IP address where the DateServer will run. If both, the DateServer and DateClient, will run on the same machine then you can use "localhost" or "127.0.0.1" as the machine name. The next step is to compile the source files DateServer.java and DateClient.java. Then run the DateServer in one window (if you are working under Windows) or in the background (if you are working under UNIX) and run the DateClient. The client should print the current date and time of the remote machine.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.