Slip No 17 Q B

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”.

FileServer.java

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

public class FileServer {

    public static void main(String[] args) {
        int port = 5000;

        try {
            ServerSocket ss = new ServerSocket(port);
            System.out.println("Server started on port " + port);

            Socket s = ss.accept();
            System.out.println("Client connected.");

            // Streams for communication
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

            // Receive filename from client
            String filename = dis.readUTF();
            File file = new File(filename);

            if (file.exists()) {
                dos.writeUTF("FOUND"); // Notify client that file exists

                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                while ((line = br.readLine()) != null) {
                    dos.writeUTF(line); // Send file line by line
                }
                br.close();
                dos.writeUTF("EOF"); // End of file marker
            } else {
                dos.writeUTF("NOT FOUND");
            }

            // Close streams and socket
            dis.close();
            dos.close();
            s.close();
            ss.close();
            System.out.println("Server terminated.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

FileClient.java

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

public class FileClient {

    public static void main(String[] args) {
        String host = "localhost";
        int port = 5000;

        try {
            Socket s = new Socket(host, port);
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            Scanner sc = new Scanner(System.in);

            System.out.print("Enter filename to check on server: ");
            String filename = sc.nextLine();

            // Send filename to server
            dos.writeUTF(filename);

            // Read server response
            String response = dis.readUTF();

            if (response.equals("FOUND")) {
                System.out.println("File Found. Contents are:");
                String line;
                while (!(line = dis.readUTF()).equals("EOF")) {
                    System.out.println(line);
                }
            } else {
                System.out.println("File Not Found");
            }

            // Close connections
            dis.close();
            dos.close();
            s.close();
            sc.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top