Hey there, so for more context this was a project for college but I thought sharing it on the internet is better than just doing it and submitting, seems to me like a better use of time and it also might help someone like me someday .
So, we will be making both the Client Side and Server Side which asks the user for the name of file they want to search for and shows them the contents of that file. So the client requests the server to send the file contents by supplying the file name. The server has some number of files. From the existing files, the server opens the requested file and the client displays the file name with the file content or basically create a Remote File Content Display System .
Analysis
When you keep this term - Remote File Content Display System aside and just think about the problem we find out that the system just requires a Server and a Client program that manage the backend and frontend respectively while we use sockets to integrate both .
After further analysis and most importantly breaking down the problem, this is what I believe each system should do to solve the problem :-
Server Side :
- It must start a new Server Socket instance on a local port .
- It should know the location of the directory where it will search for the user provided file.
- It must get all the files in the current directory into a list to make the searching easier .
- It must then search for the file linearly and if found it must use BufferedReaders and FileReaders to read the content and send it to the client using Output Streams else just send that there is no such file .
Client Side :
- It must start a Socket on the same port as the Serverβs socket to connect with it .
- It should ask the user for the name of the file he/she wants to search for and send that to the server through the Output Stream .
- It must start a Output stream to send the file name to the server.
- Then it needs to start a new data input stream to get the data that the server returns and display it to the client/user .
- At last it need to close the socket .
Flow of the System
Let's Write the Server :
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(6666); //Creating the Server Socker Instance
Socket s = ss.accept(); //Establishes Connection
System.out.println("Server is up and running..");
DataInputStream dis = new DataInputStream(s.getInputStream()); //Get's data from Client
String fileName = (String)dis.readUTF(); //Converting to String
fileName = fileName + ".txt";
File folder = new File( "C:/Users/Saumya/Desktop/JavaProject/AllFiles"); //Assingning the location to file object
File[] listOfFiles = folder.listFiles(); //Making the file content list
DataOutputStream dout = new DataOutputStream(s.getOutputStream()); //Sends data to the Client
for (File file : listOfFiles) { //Linear Searching the file
if (fileName.equalsIgnoreCase(file.getName())) {
System.out.println("Showing it to the Client :" );
System.out.println();
dout.writeUTF( "File Name : " + fileName ); //Sending the file name
dout.writeUTF( "Content : " );
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
dout.writeUTF( st ); //Sending the File Content
}
return ;
}
}
dout.writeUTF( "Sorry there is no such file !" ); //If file is no found
dout.flush();
dout.close();
ss.close(); //Shuts down the server
} catch (Exception e) {System.out.println(e);}
}
}
Let's Write the Client Side :
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 6666); //Creating the Socket with the ServerSocket Port
Scanner sc = new Scanner(System.in);
DataOutputStream dout = new DataOutputStream(s.getOutputStream()); //Sends the data to the server
System.out.println("Enter the name of file : ");
String fileName = sc.next(); //Taking the file name as input
String notFoundMsg = "Sorry there is no such file !";
dout.writeUTF( fileName ); //Sending the name to server
DataInputStream dis = new DataInputStream(s.getInputStream()); //Receives data from server
System.out.println();
String receivedData1 = (String)dis.readUTF();
System.out.println(receivedData1);
if (receivedData1.equals( notFoundMsg )) { //Stopes receiving content if file not found
s.close();
} else {
String receivedData2 = (String)dis.readUTF();
System.out.println(receivedData2);
String receivedData3 = (String)dis.readUTF();
System.out.println(receivedData3);
}
dout.flush();
dout.close();
System.out.println("Server Stoped ");
s.close(); //Stops the server
} catch (Exception e) {System.out.println(e);}
}
}
Let's see how the thing works
So, for better context I am using two Command Prompt shells one where I am running server.java
and client.java
Let's see the scenario when the searched file exists :
Server Snap :
Client Snap :
Let's see the scenario when the searched file does not exist :
Server Snap :
Client Snap :