TCP PROGRAM(Date and Time client server)
AIM
To create date and time client server using the TCP protocol
ALGORITHM
Server
- Declare the necessary arrays and variables.
- Set server port address using socket().
- Get the current message.
- Connect to the client.
- Stop the process.
Client
- Set the client machine address.
- Connect to the server.
- Read from the server the current message.
- Display the current message.
- Close the connection.
PROGRAM
DateClient:
import java.net.*;
import java.io.*;
public class DateClient
{
public static void main (String args[])
{
Socket soc;
DataInputStream dis;
String sdate ;
PrintStreamps;
try
{
InetAddressia=InetAddress.getLocalHost();
soc=new Socket(ia,8020);
dis=new DataInputStream(soc.getInputStream());
sdate=dis.readLine();
System.out.println("THE date in the serveris:"+sdate);
ps=new PrintStream(soc.getOutputStream());
ps.println(ia);
}
catch(IOException e)
{
System.out.println("THE EXCEPTION is :"+e);
}
}
}
DateServer:
import java.net.*;
import java.io.*;
importjava.util.*;
public class DateServer
{
public static void main(String args[])
{
ServerSocketss;
Socket s;
PrintStreamps;
DataInputStream dis;
String inet;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=ss.accept();
ps=new PrintStream(s.getOutputStream());
Date d=new Date();
ps.println(d);
dis=new DataInputStream(s.getInputStream());
inet=dis.readLine();
System.out.println("THE CLIENT SYSTEM ADDRESSIS :"+inet);
ps.close();
}
}
catch(IOException e)
{
System.out.println("The exception is :"+e);
}
}
}
OUTPUT:
DateClient:
C:\Program Files\Java\jdk1.6.0\bin>javac dateclient.java
C:\Program Files\Java\jdk1.6.0\bin>java dateclient
THE date in the server is:Sat Jul 19 13:01:16 GMT+05:30 2008
C:\Program Files\Java\jdk1.6.0\bin>
DateServer:
C:\Program Files\Java\jdk1.6.0\bin>javac dateserver.java
C:\Program Files\Java\jdk1.6.0\bin>java dateserver
THE CLIENT SYSTEM ADDRESS IS :com17/192.168.21.17
RESULT: Thus the output has been executed and verified successfully for date and time client server
