Attempting to perform multiple write requests using a single active socket connection to an express server running on localhost. This is done by making an HTTP request to an express web server on localhost with the following message:
GET /temp?sensorId=1&value=71 HTTP/1.1
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Ensured that the "Connection: keep-alive" header is included in the message, although not necessary when using HTTP/1.1.
Function for sending requests:
void sendRequest(char** message, int* socket_ref) {
int total = strlen(*message);
int bytes, sent = 0;
do {
bytes = write(*socket_ref, *message + sent, total - sent);
if (bytes < 0) {
perror("ERROR writing message to socket yo");
exit(EXIT_FAILURE);
}
if (bytes == 0) break;
sent += bytes;
} while (sent < total);
printf("Data sent. %d\n\n", sent);
}
Driver code snippet:
char* dummy[80] = {"GET", "localhost", "3001", "/temp", "?sensorId=1&value=71"};
setMessage(a, dummy);
makeRequest(a);
getResponse(&a);
sleep(2);
makeRequest(a);
getResponse(&a);
Expecting to receive two "71" on the express server end, but only receiving the first one. The second write request does not elicit a response even though the HTTP connection is kept alive.
Both write commands return the expected number of bytes written.
The makeRequest(a) is a wrapper calling sendRequest() as shown above.
The getRequest(&a) is a wrapper invoking read on the socket.
UPDATE The getResponse(&a) simply invokes the following code:
#define RESPONSE_SIZE 4096
char* receiveResponse(int* socket_ref) {
char* response = malloc(RESPONSE_SIZE);
int total, bytes, received = 0;
memset(response, 0, RESPONSE_SIZE);
total = RESPONSE_SIZE - 1;
do {
bytes = read(*socket_ref, response + received, total - received);
if (bytes < 0) {
perror("ERROR reading response from socket yo");
exit(EXIT_FAILURE);
}
if (bytes == 0) break;
received += bytes;
} while (received < total);
if (received == total) {
perror("ERROR storing complete response from socket yo");
exit(EXIT_FAILURE);
}
return response;
}
UPDATE 2 After calling getResponse(&a) following each makeRequest(a) call, the responses are as follows:
Data sent. 125
Response:
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Content-Length: 4
ETag: W/"4-d2PTd7kl/AFtn95E/ir6IzKjD9I"
Date: Thu, 03 Jan 2019 01:51:12 GMT
Connection: keep-alive
GOOD
Data sent. 125
Response:
However, calling getResponse(&a) after both makeRequest(a) calls result in both writes being successful, but the response is as follows:
Data sent. 125
Data sent. 125
Response:
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Content-Length: 4
ETag: W/"4-d2PTd7kl/AFtn95E/ir6IzKjD9I"
Date: Thu, 03 Jan 2019 02:00:43 GMT
Connection: keep-alive
GOODHTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Content-Length: 4
ETag: W/"4-d2PTd7kl/AFtn95E/ir6IzKjD9I"
Date: Thu, 03 Jan 2019 02:00:43 GMT
Connection: keep-alive
GOOD