Home > Back-end >  send multiple http requests using same socket
send multiple http requests using same socket

Time:11-08

I am trying to send multiple http get request to my esp32 server while using the same socket, but my program stops after printing out the response from the server. (this is a small example code i wrote thats supposed to turn on and off an led thats connected to the esp.)

This is the clients code:


URL url;
String hostname;
Socket socket;


public HttpClient() throws UnknownHostException, IOException, InterruptedException {
        url = new URL("http://192.168.178.56");
        hostname = url.getHost();
        int port = 80;
 
  
        socket = new Socket(hostname, port);
        PrintWriter output = new PrintWriter(socket.getOutputStream());
        InputStream input = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        
        for(int i=1; i<50; i  )
        {
            String x = i%2==0 ? "on" : "off";
            
            System.out.println("h1");
            
                    output.write(
                    String.format(
                        "GET /"   x   " HTTP/2\r\n"
                        "Host: "   hostname   "\r\n"
                        "Connection: keep-alive\r\n"
                        "\r\n"
                        )
                     );
                output.flush();
            
                    System.out.println("h2");
            
                    String line;

                    while ((line = reader.readLine()) != null) {
                         System.out.println(line);
                    }
            
                    System.out.println("h3");
                        
                        Thread.sleep(200);
        } 
}

and this is the console output:

h1
h2
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: text/plain
Connection: close
Accept-Ranges: none

as can be seen it always stops just before the "System.out.println("h3");"

I tried to follow the answer of a similar discussion, without success: How to do multiple http requests using the same socket connection in java?

Did I overlook something, or should I take a completely different approach?

CodePudding user response:

                    "GET /"   x   " HTTP/2\r\n"

This is not even a valid HTTP request. Doing HTTP/2 is a much more complex protocol than just using HTTP/2 as protocol string instead of HTTP/1.1 or HTTP/1.0.

  Connection: close

The server clearly says that it will close the connection after the response. So mo more requests will be accepted on the same connection.

I am trying to send multiple http get request to my esp32 server while using the same socket,

In order to send multiple requests on the same connection the server must support this. One cannot force the server to do this. Even with support for persistent connections (HTTP keep-alive) both client and server can close the connection at any time after a request response is done.

It is not uncommon that minimal HTTP stacks like used in micro controllers don't bother implementing persistent connections since it is more complex and more code.

  • Related