Wednesday, 3 May 2023

Write a Java program to check whether given file is present on server or not, if it is there then display its contents on client’s terminal otherwise display the message “File Not Found”.

 

Write a Java program to check whether given file is present on server or not, if it is  there then display its contents on client’s terminal otherwise display the message “File  Not Found”.

 Serverside file

 

import java.io.*;

import java.net.*;

 

 public class Server {

 

     public static void main(String[] args) throws Exception {

     ServerSocket ss = new ServerSocket(5400);

        while (true) {

 

            Socket s = ss.accept();

 

            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

 

            File f = new File("c:\a-java\src\jsp\server.text");

            if (f.exists()) {

                Scanner sc = new Scanner(f);

                while (sc.hasNextLine()) {

 

                     String str = sc.nextLine();

 

                     dos.writeUTF(f.getName() + " = file content = " + str);

 

                 }

 

 

            } else {

 

                String stra = "file is not Exists on SERVER";

 

                dos.writeUTF(" Error -- " + stra);

 

            }

 

        }

 

    }

 

}

 

 

clientside file

 

 

import java.net.*;

 

import java.io.*;

 

 public class Client {

 

    public static void main(String[] args) throws IOException {

        Socket s = new Socket("localhost", 5400);

 

         DataInputStream dis = new DataInputStream(s.getInputStream());

 

         System.out.print("result = ");

         System.out.print(dis.readUTF());

 

    }

 

}