1

I'm using com.sun.net.HttpServer class to build a http server with java like the following:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.Executors;

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

public class SimpleHttpServer {

  public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(8989), 0);
    server.createContext("/", new MyHandler());
    server.setExecutor(null); // creates a default executor
    server.start();
  }

  static class MyHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {

        //Read the request
        InputStream in = httpExchange.getRequestBody();
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer);
        String inputString = writer.toString();

        //prepare the response
        httpExchange.sendResponseHeaders(200, "Hi my faithful client".length());
        OutputStream os = httpExchange.getResponseBody();
        os.write("Hi my faithful client".getBytes());
        os.close();
    }
  }
}

I'm communicating with this server using this client:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String argv[]) throws IOException{
        String urlstr = "http://127.0.0.1:8989";
        URL url = new URL(urlstr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write("Hello HTTP server!! I'm your client1");

        InputStream in  = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder result = new StringBuilder();
        String line;
        while((line = reader.readLine()) != null) {
            result.append(line);
            System.out.println(result.toString());
        }
    }
}

and it'fine working. but what I want now is that the server allows the client to communicate with him for a session that means a sequence of request/response not just only one. So it will be a loop of request/response. For this purpose I tried to add as a first step just one request to the client by adding to it those two lines:

writer.flush();
writer.write("Hello HTTP server!! I'm your client2");

But it doesn't work. Just the first request is caught by the server.

How can I change the code to achieve my purpose?

3
  • please I need an answer. and thank you in advance. Commented Jul 27, 2016 at 8:43
  • Do you need to perform just another request or you need a session management with cookies, for example. Commented Jul 27, 2016 at 9:50
  • @CássioMazzochiMolin it's a session. just a simple sequence of request of request/response between the client and the server. the other request that I added to the client in my example is just a try of a session of two requests. If it works for 2 requests, so it can also for 3, 4, ... with the same way Commented Jul 27, 2016 at 10:10

1 Answer 1

1

Please check the code below:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test1 {

static String urlstr = "http://127.0.0.1:8989";
public static void main(String argv) throws IOException{
    URL url = new URL(urlstr);
    Test1 t = new Test1();
    for (int i = 0; i < 10; i++) {
        t.sendRequest("Hello HTTP server!! I'm your client" + i, url);
    }

}

private void sendRequest(String strToSend, URL url) throws IOException{

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);

    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(strToSend);

    InputStream in  = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder result = new StringBuilder();
    String line;
    while((line = reader.readLine()) != null) {
        result.append(line);
        System.out.println(result.toString());
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.