@ Christian, did I write the program second time according to your suggestion or is there any confusion/misunderstanding between you and me? Could you please send me a reply? Any suggestion from any evhttp user/expert? Thanks.
________________________________ From: Zulfiqer Sekender <[email protected]> To: [email protected] Sent: Sun, December 12, 2010 12:13:16 AM Subject: Re: [Libevent-users] How http client can send back message to server? 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; } ________________________________ From: Christian Dahlqvist <[email protected]> To: [email protected] Sent: Sat, December 11, 2010 9:19:22 AM Subject: Re: [Libevent-users] How http client can send back message to server? I am a bit confused about your description how the flow of messages between client and server works. It seems like the client is trying to act as a server once the first request has been sent, which I would expect would confuse any standard HTTP server. Should the client not instead send a new request containing the heartbeat acknowledgement instead of a HTTP response? Christian On Fri, Dec 10, 2010 at 5:18 PM, Zulfiqer Sekender <[email protected]> wrote: > 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; > > } > > > > > *********************************************************************** To unsubscribe, send an e-mail to [email protected] with unsubscribe libevent-users in the body.
