100% found this document useful (1 vote)
87 views4 pages

TCP Date / Time Server

This document describes a client-server program to obtain the system date and time using TCP. The client sends a date or time request to the server. The server responds with the date or time using the Gregorian calendar and closes the connection. The client receives the response, prints it, and closes the connection. The program includes the client and server code in Java using sockets to establish the connection and send/receive data.

Uploaded by

eldhosejm
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
87 views4 pages

TCP Date / Time Server

This document describes a client-server program to obtain the system date and time using TCP. The client sends a date or time request to the server. The server responds with the date or time using the Gregorian calendar and closes the connection. The client receives the response, prints it, and closes the connection. The program includes the client and server code in Java using sockets to establish the connection and send/receive data.

Uploaded by

eldhosejm
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX.

NO:1 DATE :

TCP DATE / TIME SERVER


AIM:
To obtain the system date and time of server using TCP.

ALGORITHM:
CLIENT: 1. Start and import all the necessary packages. 2. Create a client side socket with server address. 3. Send date or time request to server. 4. Get response from socket and point the result. 4. Close the socket and stop. SERVER: 1. Start and import all the necessary packages. 2. Create server socket and accept client. 3. Give response using Gregorian calendar. 4. Close the socket and stop.

PROGRAM: CLIENT:
import java.io.*;

import java.net.*; public class dtclient { public static void main(String args[])throws IOException { String rec; String res; try { Socket s=new Socket("127.0.0.1",8081); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedReader brl=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw=new PrintWriter(s.getOutputStream(),true); do { rec=br.readLine(); pw.println(rec); res=brl.readLine(); System.out.println(res); } while(!rec.equalsIgnoreCase("End")); } catch(Exception e) { System.out.println("Error"+e); } } }

SERVER:
import java.io.*; import java.net.*; import java.util.*; public class dtserver { public static void main(String args[])throws IOException {

try { String com; ServerSocket ss=new ServerSocket(8081); Socket s=ss.accept(); System.out.println("Connected"); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw=new PrintWriter(s.getOutputStream(),true); do { com=br.readLine(); System.out.println(com); if(com.equalsIgnoreCase("Date")) { GregorianCalendar c=new GregorianCalendar(); pw.println(c.get(Calendar.DATE)+"/"+(c.get(Calendar.MONTH) +"/"+(c.get(Calendar.YEAR)))); } else if(com.equalsIgnoreCase("Time")) { GregorianCalendar t=new GregorianCalendar(); t.setTime(new Date()); pw.println(t.get(Calendar.HOUR)+"/"+(t.get(Calendar.MINUTE) +"/"+(t.get(Calendar.SECOND)))); } else { pw.println("Wrong"); } } while(!com.equalsIgnoreCase("End")); } catch(Exception e) { } } }

OUTPUT:
C:\jdk1.3\bin>javac dtclient.java C:\jdk1.3\bin>javac dtserver.java C:\jdk1.3\bin>java dtserver

2nd C:\jdk1.3\bin>java dtclient date 16/8/2006 time 11/43/57 end Wrong C:\jdk1.3\bin> 1st C:\jdk1.3\bin>java dtserver Connected date time end C:\jdk1.3\bin>

You might also like