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

  • SEARCH
  • Home
  • 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 1095407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:07:09+00:00 2026-05-17T00:07:09+00:00

This is my code on the client side: import java.io.*; import java.net.*; public class

  • 0

This is my code on the client side:

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

public class httpClient {

    public void TcpSocket()
    {
        String sentence;
        String modifiedSentence;
        StringBuffer contents= null;

        //open a socket connection on port 80
        try{
            Socket clientSocket = new Socket("localhost", 8080);

            //send message to the server
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

            //read message from the server
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            //read http request message from a file
            File file = new File("/home/x/Desktop/test.txt");
            contents = new StringBuffer();     
            BufferedReader reader = null;
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null)
            {
                contents.append(text).append(System.getProperty("line.separator"));
            } 
            //end reading file

            //Send message
            sentence = contents.toString();
            outToServer.writeBytes(sentence + '\n');
            modifiedSentence = inFromServer.readLine();
            System.out.println("FROM SERVER: " + modifiedSentence);

            clientSocket.close();
        }
        catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }

    }

    public static void main(String args[])
    {
        httpClient cl = new httpClient();
        cl.TcpSocket();
    }
}

and my http server:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class HttpServerDemo {
  public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(8080);
    HttpServer server = HttpServer.create(addr, 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();
    System.out.println("Server is listening on port 8080" );
  }
}

class MyHandler implements HttpHandler {
  public void handle(HttpExchange exchange) throws IOException {
    System.out.println("I am in Server request Handler"); <---------------Request is not coming here
    String requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("GET")) {
      Headers responseHeaders = exchange.getResponseHeaders();
      responseHeaders.set("Content-Type", "text/plain");
      exchange.sendResponseHeaders(200, 0);

      OutputStream responseBody = exchange.getResponseBody();
      Headers requestHeaders = exchange.getRequestHeaders();
      Set<String> keySet = requestHeaders.keySet();
      Iterator<String> iter = keySet.iterator();
      while (iter.hasNext()) {
        String key = iter.next();
        List values = requestHeaders.get(key);
        String s = key + " = " + values.toString() + "\n";
        responseBody.write(s.getBytes());
      }
      responseBody.close();
    }
  }
}

Request I am sending:

GET /index.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3
Accept: /
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

But my request is not coming in to HttpServerDemo.java’s request handler.

Update

I am not able to debug my code since it’s not hitting the request handler on the server and it works fine in a real browser. This is the response from the real browser when I open http://localhost:8080

Host = [localhost:8080]
Accept-charset = [ISO-8859-1,utf-8;q=0.7,*;q=0.7]
Accept-encoding = [gzip,deflate]
Connection = [keep-alive]
Keep-alive = [300]
Accept-language = [en-us,en;q=0.5]
User-agent = [Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3]

Accept = [text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
  • 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-05-17T00:07:10+00:00Added an answer on May 17, 2026 at 12:07 am

    You forgot a blank newline between request headers and request body. The server now thinks that there are more headers to come and is waiting with handling the response. You must always insert a blank newline (CRLF) after the request headers. See also Wikipedia: HTTP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I need to build some client side code which follows this use case: An
I typically do not develop client-side (in this case, mobile) code, so my apologies
Can anybody explain what does JdClient is in this code: public JdClient client =
I'm trying to make a multithreaded server/client app with java ! this code is
I wrote this code in client side and sent to a server for a
If i have written this code on Client Side then does my communication is
I have this code in the client side: $.post('<%=Url.Action(Action_Name,Controller_Name)%>', { serverParam: clientParam}, null, null);
Using this code on the client side: <script src=//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js></script> <script src=http://cdn.socket.io/stable/socket.io.js></script> <script type=text/javascript> $(document).ready(function
I've generated a web service client using cxf-codegen-plugin, and locally I'm running this code

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.