Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday, 13 July 2011

Program for Client - Server Communication to Display date using Java


     This is a program for displaying the date from server to client. Client is requesting the server to retrieve the current date.

Client:

import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
            public static void main(String args[])
            {
            try
            {
                        Socket clsct=new Socket("127.0.0.1",139);

                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
                        String str=din.readLine();
                        dout.writeBytes(str+'\n');
                        System.out.println(str);                      
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}

Server:

import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                       
                        while(true)
                        {
                                    Socket obj1=obj.accept();
                                   
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    Date d=new Date();
                                    String S=d.toString();
                                    dout.writeBytes(S+'\n');
                       
                                    obj.close();
                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}

Output

E:\networks>javac Server.java

E:\networks>javac Client.java

E:\networks>java Server

E:\networks>java Client
Tue Jul 12 22:25:06 PDT 2011

No comments:

Post a Comment

Followers