This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Send multiple http post request to the same server consecutively

We are using over own server that host multiple apis for different functionalities.

I am able to make http post requests to all of them individually and everything works fine. 

For efficient flow of the program (more importantly from power consumption stand point) it is much better if I can open one socket connection to our server and send multiple http post requests one after and another and then close the connection (socket) at the end. This is where I am facing a challenge. 

This works:
create a socket, connect, send for 1st api, close socket .... create a socket, connect, send for 2nd api, close socket  .... create a socket, connect, send for 3rd api, close socket 

What does not work:

create a socket, connect, send for 1st api, send for 2nd api, send for 3rd api, close socket 

when sending the 2nd http post data, i get a 128 error (ENOTCONN 128 /* Socket is not connected */).

I have tried with keep-alive in my request header but that waits for 5-10 seconds after sending before a response is received from our server (which is a big NO for power consumption)

here is my header:

"POST /api1/HTTP/1.1\r\n"\
"Host: xxx.com \r\n"\
"Connection: keep-alive\r\n"\
"Content-Type: application/json\r\n"\
"Content-Length: %d\r\n\r\n"\
"%s\r\n"

Thanks in advance!

  • Hi,

    What is the server running?

    We have seen similar behavior when downloading FOTA images from an Apache server. The solution in those cases was to increase the KeepAliveTimeout from 5 to 10 in /etc/apache2/apache2.conf.

    In the above case, they were using the download_client library to download a FOTA image piecewise with repeated GET requests. After they looked at the IP traffic on the server side, they saw that the requests came with 4-6 seconds intervals.

    If timeouts in the server isn't your issue, could you take a modem trace, to help me figure out why the connection gets closed?

    Best regards,

    Didrik

Related