DATE AND TIME SERVER
AIM:
TO implement date and time display from local host to server using TCP
ALGORITHM: CLIENT
1.start the program
2. Include necessary package in java
3. To create a socket in client to server.
4. the client connection accept to the server and replay to read the system
date and time.
5. Stop the program.
ALGORITHM: SERVER
1.start the program
2. Include necessary package in java
3. To create a socket in server to client.
4. To display the current date and time to client
5. Stop the program.
Program :
DATECLIENT:
import java.net.*;
import java.io.*;
class dateclient
public static void main (String args[])
Socket soc;
DataInputStream dis;
String sdate;
PrintStream ps;
try
InetAddress ia=InetAddress.getLocalHost();
soc=new Socket(ia,8020);
dis=new DataInputStream(soc.getInputStream());
sdate=dis.readLine();
System.out.println("THE date in the server is:"+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.*;
import java.util.*;
class dateserver
public static void main(String args[])
{
ServerSocket ss;
Socket s;
PrintStream ps;
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 ADDRESS IS :"+inet);
ps.close();
catch(IOException e)
{
System.out.println("The exception is :"+e);
OUTPUT:
CLIENTSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac dateclient.java
Note: dateclient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.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.5.0\bin>
SERVERSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac dateserver.java
Note: dateserver.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateserver
THE CLIENT SYSTEM ADDRESS IS :com17/192.168.21.17
RESULT:
Thus the above program a date and time display from local host to server
using TCP was executed and successfully
CLIENT-SERVER APPLICATION FOR CHAT
AIM:
To write a client-server application for chat using TCP
ALGORITHM: CLIENT
1.start the program
2. Include necessary package in java
3. To create a socket in client to server.
4. The client establishes a connection to the server.
5. The client accept the connection and to send the data from client to server
and vice versa
6. The client communicate the server to send the end of the message
7. Stop the program.
ALGORITHM: SERVER
1.start the program
2. Include necessary package in java
3. To create a socket in server to client
4. The server establishes a connection to the client.
5. The server accept the connection and to send the data from server to client
and vice versa
6. The server communicate the client to send the end of the message
7. Stop the program.
TCPserver1.java
import java.net.*;
import java.io.*;
public class TCPserver1
public static void main(String arg[])
{
ServerSocket s=null;
String line;
DataInputStream is=null,is1=null;
PrintStream os=null;
Socket c=null;
try
s=new ServerSocket(9999);
catch(IOException e)
System.out.println(e);
try
c=s.accept();
is=new DataInputStream(c.getInputStream());
is1=new DataInputStream(System.in);
os=new PrintStream(c.getOutputStream());
do
line=is.readLine();
System.out.println("Client:"+line);
System.out.println("Server:");
line=is1.readLine();
os.println(line);
}while(line.equalsIgnoreCase("quit")==false);
is.close();
os.close();
catch(IOException e)
System.out.println(e);
TCPclient1.java
import java.net.*;
import java.io.*;
public class TCPclient1
public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
c=new Socket("127.0.0.1",9999);
catch(IOException e)
System.out.println(e);
try
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
do
System.out.println("Client:");
line=is.readLine();
os.println(line);
System.out.println("Server:" + is1.readLine());
}while(line.equalsIgnoreCase("quit")==false);
is1.close();
os.close();
catch(IOException e)
System.out.println("Socket Closed!Message Passing is over");
OUT PUT :
Server
C:\Program Files\Java\jdk1.5.0\bin>javac TCPserver1.java
Note: TCPserver1.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java TCPserver1
Client: Hai Server
Server:
Hai Client
Client: How are you
Server:
Fine
Client: quit
Server:
quit
Client
C:\Program Files\Java\jdk1.5.0\bin>javac TCPclient1.java
Note: TCPclient1.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java TCPclient1
Client:
Hai Server
Server: Hai Client
Client:
How are you
Server: Fine
Client:
quit
Server: quit
RESULT:
Thus the above program a client-server application for chat using
TCP / IP was executed and successfully
IMPLEMENTATION OF TCP/IP ECHO
AIM:
To implementation of echo client server using TCP/IP
ALGORITHM:
1.start the program
2. Include necessary package in java
3. To create a socket in client to server.
4. The client establishes a connection to the server.
5. The client accept the connection and send data to server and the server to
replay the echo message to the client
6. The client communicate the server to send the end of the message
7. Stop the program.
Program :
EServer.java
import java.net.*;
import java.io.*;
public class EServer
public static void main(String args[])
ServerSocket s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
s=new ServerSocket(9000);
catch(IOException e)
{
System.out.println(e);
try
c=s.accept();
is=new DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
line=is.readLine();
ps.println(line);
catch(IOException e)
System.out.println(e);
}
EClient.java
import java.net.*;
import java.io.*;
public class EClient
public static void main(String arg[])
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
c=new Socket("127.0.0.1",9000);
catch(IOException e)
System.out.println(e);
try
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());
while(true)
System.out.println("Client:");
line=is.readLine();
os.println(line);
System.out.println("Server:" + is1.readLine());
catch(IOException e)
System.out.println("Socket Closed!");
}
Output
Server
C:\Program Files\Java\jdk1.5.0\bin>javac EServer.java
Note: EServer.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EServer
C:\Program Files\Java\jdk1.5.0\bin>
Client
C:\Program Files\Java\jdk1.5.0\bin>javac EClient.java
Note: EClient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java EClient
Client:
Hai Server
Server:Hai Server
Client:
Hello
Server:Hello
Client:
end
Server:end
Client:
ds
Socket Closed!
RESULT:
Thus the above program a simple echo client-server application for
using TCP / IP was executed and successfully