Write a program to retrieve date and time using TCP.

Code for Program of date server and client in Java


// Date Client 

import java.io.*;
import java.net.*;

class DateClient
{
    publicstaticvoid main(String args[]) throws Exception
    {
        Socket soc=new Socket(InetAddress.getLocalHost(),5217);        
        BufferedReader in=new BufferedReader(
                new InputStreamReader(
                        soc.getInputStream()
                        )
                    );

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



// Date Server

import java.net.*;
import java.io.*;
import java.util.*;

class DateServer
{
    publicstaticvoid main(String args[]) throws Exception
    {
        ServerSocket s=new ServerSocket(5217);

        while(true)
        {
            System.out.println("Waiting For Connection ...");
            Socket soc=s.accept();
            DataOutputStream out=new DataOutputStream(soc.getOutputStream());
            out.writeBytes("Server Date" + (new Date()).toString() + "\n");
            out.close();
            soc.close();
        }
        
    }
}