Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:27:00+00:00 2026-06-11T05:27:00+00:00

So after establishing my listening for a connection and accepting one: ServerSocket serverSock =

  • 0

So after establishing my listening for a connection and accepting one:

ServerSocket serverSock = new ServerSocket(6789);
Socket sock = serverSock.accept();

When I type into my browser localhost:6789/index.html how do I handle this incoming GET request and return index.html? index.html is in the same directory.

Firstly I want to very that index.html actually exists and if not I return a HTTP 404 message. Then I will close the connection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:27:01+00:00Added an answer on June 11, 2026 at 5:27 am

    Handling GET and other requests is actually very simple but you must know the specification of the HTTP protocol.

    The first thing to do is to get the SocketInputStream of the client and the path of the file to return. The first line of the HTTP request comes in this form: GET /index.html HTTP/1.1. Here is a code example that does that:

    SocketInputStream sis = sock.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(sis));
    String request = br.readLine(); // Now you get GET index.html HTTP/1.1
    String[] requestParam = request.split(" ");
    String path = requestParam[1];
    

    You create a new File object and checks if that file exists. If the file does not exist, you return a 404 response to the client. Otherwise you read the file and send its content back to the client:

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
    File file = new File(path);
    if( !file.exist()){
      out.write("HTTP 404") // the file does not exists  
    }
    FileReader fr = new FileReader(file);
    BufferedReader bfr = new BufferedReader(fr);
    String line;
    while((line = bfr.readLine()) != null){
      out.write(line);
    }
    
    bfr.close();
    br.close();
    out.close();    
    

    Here is the complete code summary:

    ServerSocket serverSock = new ServerSocket(6789);
    Socket sock = serverSock.accept();
    
    InputStream sis = sock.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(sis));
    String request = br.readLine(); // Now you get GET index.html HTTP/1.1`
    String[] requestParam = request.split(" ");
    String path = requestParam[1];
    
    PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
    File file = new File(path);
    if (!file.exists()) {
         out.write("HTTP 404"); // the file does not exists
    }
    FileReader fr = new FileReader(file);
    BufferedReader bfr = new BufferedReader(fr);
    String line;
    while ((line = bfr.readLine()) != null) {
        out.write(line);
    }
    
    bfr.close();
    br.close();
    out.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying TCP file transfer on Linux. After establishing the connection, the server should
I am working on a mobile communicator and after establishing connection to the server
I am getting the error: error establishing a database connection after moving my wordpress
After debugging a CodeIgniter application that were installed into a new development environment, I
After establishing a remote connection to a MySQL server (using the MySQL command-line front-end)
I have a case where the client after establishing a connection with the server,
After discovering about Javascript namespaces, I tried to implement them but I run into
I am establishing a connection to a secured server over SSL. Everything works fine,
I am getting some strange character displays on hyper terminal, after establishing communication with
I am creating a cryptographically-secure IM application in Java. The first step (after establishing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.