[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] How http client can send back message to server?



Hi,

I have to develop a simple http client which can communicate with a http server. The client is sending evhttp_make_request(). The server sends "HT:" heartbeat in response after certain time and when the client gets "HT:" each time it will send "HA:" as heartbeat acknowledgement. Transfering of "HT:" & "HA:" should go on.

I developed the following client program. It gets "HT:" and tries to send back "HA:". But server never gets the "HA:".

I will highly appreciate any help regarding this. Please, let me know what wrong I am doing and how to do it correctly.  (Intentionally I left few commented out lines to show what else options I tried.)

Please, please help me.

Thanks.


Here is the code:

 

#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:");

      evbuffer_add_printf(req->output_buffer,"HA:");

     // evhttp_send_reply_start(req, HTTP_OK, "Start");

      evhttp_send_reply_chunk(req, evbuff);

      // evhttp_send_reply_chunk(req,"3/r/nHA:/r/n");

     // evhttp_send_reply(req, HTTP_OK, "", evbuff);

     // evhttp_send_reply(req, HTTP_OK, "Response","3/r/nHA:/r/n" );

     // evhttp_send_reply(req, HTTP_OK, "Response",NULL);

      // evhttp_send_reply_end(req);

      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;

}