I installed libevent-2.0.9-rc and executed the following program. It gave me a bit better result, but didnât solve my problem completely. Now client can send the first HA: (Heartbeat Acknowledgement) after it gets the first heartbeat and server gets that. But for some reason client doesnât get the second heartbeat. From server log, it is confirmed that server sends the second heartbeat. But I donât know why my client doesnât get the second heartbeat.
According to Christianâs suggestion, I tried the second program (which is also attached at the end of this mail) but in that case server even doesnât get the first heartbeat acknowledgement.
I will highly appreciate any help regarding this. Please, let me know how to do it correctly.
Please, please help me.
Thanks.
#include <sys/types.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <stdlib.h>
//#include <err.h>
#include <event.h>
#include <evhttp.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
#define BUFSIZE 4096
#define SERVERADDR "15.172.175.61"
#define SERVERPORT 8070
#define SLEEP_MS 10
char buf[BUFSIZE];
int bytes_recvd = 0;
int chunks_recvd = 0;
int closed = 0;
int connected = 0;
// called per chunk received
void chunkcb(struct evhttp_request * req, void * arg)
{
int s = evbuffer_remove( req->input_buffer, &buf, BUFSIZE );
printf("Read %d bytes: %s\n", s, &buf);
bytes_recvd += s;
chunks_recvd++;
if (strcmp(buf,"HT:") == 0)
{
struct evbuffer *evbuff = NULL;
evbuff = evbuffer_new();
evbuffer_add_printf(evbuff,"HA:");
evhttp_send_reply_chunk(req, evbuff);
evbuffer_free(evbuff);
}
}
// gets called when request completes
void reqcb(struct evhttp_request * req, void * arg)
{
closed++;
printf("\nClosed.\n");
}
int main(int argc, char **argv)
{
event_init();
struct evhttp *evhttp_connection;
struct evhttp_request *evhttp_request;
char addr[16];
char path[3200]; // eg: "/test/123"
sprintf(&addr, "12.176.173.18");
evhttp_connection = evhttp_connection_new(SERVERADDR, SERVERPORT);
if (evhttp_connection == NULL)
{
printf("New connection failed\n");
break;
}
evhttp_connection_set_local_address(evhttp_connection, &addr);
evhttp_set_timeout(evhttp_connection, 864000); // 10 day timeout
evhttp_request = evhttp_request_new(reqcb, NULL);
evhttp_request->chunk_cb = chunkcb;
evhttp_request->chunked = 1; // Chunked
evhttp_add_header(evhttp_request->output_headers, "Transfer-Encoding", "chunked");
sprintf(&path, "/test/123");
evhttp_make_request( evhttp_connection, evhttp_request, EVHTTP_REQ_POST, path );
evhttp_connection_set_timeout(evhttp_request->evcon, 864000);
event_loop( EVLOOP_NONBLOCK );
usleep(SLEEP_MS*1000);
event_dispatch();
return 0;
}
Program according to Christianâs suggestion :
#include <sys/types.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <stdlib.h>
//#include <err.h>
#include <event.h>
#include <evhttp.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
#define BUFSIZE 4096
#define SERVERADDR "15.172.175.61"
#define SERVERPORT 8070
#define SLEEP_MS 10
char buf[BUFSIZE];
struct evhttp *evhttp_connection;
char path[3200];
int bytes_recvd = 0;
int chunks_recvd = 0;
int closed = 0;
int connected = 0;
// gets called when request completes
void reqcb(struct evhttp_request * req, void * arg)
{
closed++;
printf("\nClosed.\n");
}
// called per chunk received
void chunkcb(struct evhttp_request * req, void * arg)
{
int s = evbuffer_remove( req->input_buffer, &buf, BUFSIZE );
printf("Read %d bytes: %s\n", s, &buf);
bytes_recvd += s;
chunks_recvd++;
if (strcmp(buf,"HT:") == 0)
{
evhttp_request = evhttp_request_new(reqcb, NULL);
evhttp_request->chunk_cb = chunkcb;
evhttp_request->chunked = 1; // Chunked
// evhttp_add_header(evhttp_request->output_headers, "Transfer-Encoding", "chunked");
evbuffer_add_printf(evhttp_request->output_buffer,"HA:");
evhttp_make_request( evhttp_connection, evhttp_request, EVHTTP_REQ_POST, path );
}
}
int main(int argc, char **argv)
{
event_init();
struct evhttp *evhttp_connection;
struct evhttp_request *evhttp_request;
char addr[16];
char path[3200]; // eg: "/test/123"
sprintf(&addr, "12.176.173.18");
evhttp_connection = evhttp_connection_new(SERVERADDR, SERVERPORT);
if (evhttp_connection == NULL)
{
printf("New connection failed\n");
break;
}
evhttp_connection_set_local_address(evhttp_connection, &addr);
evhttp_set_timeout(evhttp_connection, 864000); // 10 day timeout
evhttp_request = evhttp_request_new(reqcb, NULL);
evhttp_request->chunk_cb = chunkcb;
evhttp_request->chunked = 1; // Chunked
evhttp_add_header(evhttp_request->output_headers, "Transfer-Encoding", "chunked");
sprintf(&path, "/test/123");
evhttp_make_request( evhttp_connection, evhttp_request, EVHTTP_REQ_POST, path );
evhttp_connection_set_timeout(evhttp_request->evcon, 864000);
event_loop( EVLOOP_NONBLOCK );
usleep(SLEEP_MS*1000);
event_dispatch();
return 0;
}