We help IT Professionals succeed at work.
Get Started
Private
Troubleshooting Question

Writing a server using Sockets that lists all files in the current directory

Rohit Bajaj
Rohit Bajaj asked
on
37 Views
Last Modified: 2020-11-17
HI,
I have currently a very basic startup code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class JokeServer {
    public static void main(String a[]) throws IOException {
        // Using try with resources as it closes everything autocloseable
        try (
                ServerSocket serverSocket = new ServerSocket(Constants.jokeServerPort, Constants.backLog);
                Socket socket = serverSocket.accept();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        ) {
            printWriter.println("Hello World");
//        JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();

        }
    }

}


This server runs on localhost and port 4545
I opened the url http://localhost:4545/ in chrome browser
The server was waiting for the request and after i hit this url the server exited.
Which is understandable. But the Hello World didnt get printed on the chrome page.

What i actually want to achieve is when this url is hit. Then chrome displays all the files and folder in the directory where server was started (Kind of like an FTP server)
Not only that the user should be able to click on files which opens in chrome
or click on directories which list down the files inside it or may be something simpler...

Any help on the above. How do i go about achieving this.
Any links/videos etc...

Specially if you can point  how do i list files in the current directory which open when it is clicked... That should be a good beginning point.

Thanks

Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE