0

I am trying to send some HTTP requests continuously to a server using Java.

Socket socket = new Socket("ip.address", port);
socket.setKeepAlive(true);  //it does not help
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));

for(int i=1;i<50;i++){

    String data = "some data " + i;
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");
    wr.write(data);
    wr.flush();

}
wr.close();
socket.close();

The server receives only the first request, then the program terminates by throwing the exception

java.net.SocketException: Software caused connection abort: socket write error

So, I tried this way, creating socket & closing inside the loop every time:

for(int i=1;i<50;i++){
    Socket socket = new Socket("ip.address", port);
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));

    String data = "some data " + i;
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");
    wr.write(data);
    wr.flush();

    wr.close();
    socket.close();
}

It works fine as I had expected. The server receives all the requests.

Questions:

  1. Is the second approach correct way to do? If I open & close every time, will it not affect the performance in general? Or How can I improve the first approach?

  2. Is there any other better approach to send HTTP requests? (It does not have to be using sockets). Performance is VERY important for us.

3
  • I believe that by using a socket once, and changing the buffer, you eradicate it's use. So one would expect that you migt send more than one integer in th buffer. But otherwise you have to reestablish the connection. Commented Oct 15, 2015 at 16:47
  • Try adding the header Connection: keep-alive to the first example and see if it works any better for you. Commented Oct 15, 2015 at 16:48
  • @RealSkeptic, already tried. It does not help. Commented Oct 15, 2015 at 16:49

3 Answers 3

4

The client and server have to be in agreement as to what data is expected over the connection, i.e they both have to adhere to the application layer protocol, which in this case is HTTP.

HTTP 1.1 does allow multiple requests per connection, but your client and server must be coordinated properly. For example if the server sends a response the client may have to read it before sending the next request etc.

It also appears that you are specifying HTTP 1.0 in the header, which does not have well-defined support for multiple requests per connection.

In short, on a modern web server using the HTTP 1.1 protocol you can send multiple requests per request as long as the client is writing/reading data consistently with the protocol definition.

Also, be aware of the difference between a TCP (socket) keepalive and the HTTP keep-alive: The TCP keepalive manages the connection itself, the HTTP keep-alive defines the semantics of the data exchange across the connection.

A good tool for HTTP clients in Java is the Apache HttpComponents library. The library will take care of all the HTTP details, such as headers, connection settings, etc.

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

3 Comments

Thanks. I am not worried about the response actually. Just send the request & forget - What is the correct way for this?!
The Apache library I referenced above lets you easily send requests with just a few lines of code. See the examples for the core components to see how to send simple GETs and POSTs. (You may need to read the response even if you don't use it...) If you are just trying to hit the server with many requests for load testing, you can use jMeter
Awesome. I will check now.
1

In the 2nd approach you open a new connection each time and send the data to the server. In the 1st approach you re-use the connection, the server does not expect this and closes the connection. So everything works as expected here.

1 Comment

Thanks. I just wanted to know if I am going in the right direction.
1

You should read about Keep-Alive Header HTTP 1.1 is stateless by default, but it has option for connection management. It is not so naive and simple as just adding one header. I suggest you use some library e.g. apache httpclient (see PoolingClientConnectionManager)

EDIT

You should read server answer after POST and check response headers when you try use keep-alive header.

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.