Exp8 MCN

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Name: Trusha Neogi Date: 1/7/22

Roll No: 191105082


Branch: TE Comp

EXPERIMENT 8

TITLE: Client Server Program to display Server Machine Date and Time on the Client
Machine

AIM: To implement Client Server Program to display Server Machine Date and Time on
the Client Machine using Java

THEORY: In this program, we make use of sockets. We use java.net, java.io and
java.util packages. The java.net package provides with classes for implementing the
networking related application. Java.io is concerned with the system input and output
operations as well as classes such as DataOutputStream, DataInputStream,
BufferedReader, etc. Java.util provides creation of applets as well classes that facilitate
communication between different applets.

Two programs are required, the Server program and the Client program. The Serve
Program contains a class called DateServer in which a ServerSocket object from
java.net is created and connected a given port number, here port number is 5217.

Then, a while loop runs which waits for a connection with the port. An object soc of
class Socket is initialised to get the connection of the socket to the port number given.
An object of the DataOutputStream is initialised to get the output stream from the
socket object. The writeBytes method will pass this output stream data as a string to
the client side program. The objects are then closed.

In the Client side program, DateClient, an object soc of Socket class is initialised and
passed the same localhost address (port number) which the server program connected
to. It uses a BufferedReader to convert the string output obtained from the server
program into easily readable text. The system then prints this output.

CODE

DateClient.java

import java.io.*; //provides for input/output for system


import java.net.*;
class DateClient
{
public static void main(String args[]) throws Exception
{
Socket soc=new Socket(InetAddress.getLocalHost(),5217); //create a socket
and pass localhost address
BufferedReader in=new BufferedReader(new
InputStreamReader(soc.getInputStream() )); //BufferedReader converts characters
to text

Trusha Neogi 191105082 Batch E


System.out.println(in.readLine());
}
}

DateServer.java

import java.net.*; //provides classes for implementing network related application


import java.io.*; //system ip/op, dtaa stream, serialization, file system
import java.util.*; //for applet, creation and classes for applet to communicate with
other applets
//DateServer will send data to client side and client side will read it
class DateServer
{
public static void main(String args[]) throws Exception
{
ServerSocket s=new ServerSocket(5217); //object from java.net, creates server
socket on port number 5217
while(true)
{
System.out.println("Waiting For Connection ...");
Socket soc=s.accept(); //object of class Socket used to connect to port
number, accept certain value from ServerSocket obj s
DataOutputStream out=new DataOutputStream(soc.getOutputStream());
out.writeBytes("Server Date: " + (new Date()).toString() + "\n"); //passing
Server date to the client side
out.close(); //close data
soc.close(); //close socket
}
}
}

OUTPUT

Trusha Neogi 191105082 Batch E


CONCLUSION: Client Server Program to display Server Machine Date and Time on the
Client Machine was successfully implemented.

Trusha Neogi 191105082 Batch E

You might also like