Well, you are using HTTP 1.0
In this version, after the server sends the response, it will close the
connection.
You may use HTTP 1.1 with "Connection: keep-alive" header, to signal the
server not to close the connection.

On Tue, Jul 21, 2020, 19:24 Bill Engelke <engelk...@bellsouth.net> wrote:

> I need code that will send a "heartbeat" to a web service (say, once per
> minute), and receive a response. This works fine with a browser, so I know
> the server is reachable and responding correctly. I have tried a vast
> number of different ways of doing this using libuv and c code, and so far,
> partial success at best. I have been able to connect to the server and send
> a request; but only the first request would work, and further sends would
> not make it to the server.  In my most recent attempts, even the connection
> request stopped working. I don't know if the problem is my incorrect use of
> libuv, or something in the networking part that I don't understand.  I have
> put the code into a single app for testing below; can anyone offer any
> suggestions??
>
> ```
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <uv.h>
> #include <string.h>
> #include <unistd.h>
>
> typedef struct {
>   uv_write_t req;
>   uv_buf_t buf;
>  } write_req_t;
>
> static uv_tcp_t* socket_central;
> static struct sockaddr_in central_dest;
>
> static void alloc_buffer(uv_handle_t* handle, size_t suggested_size,
> uv_buf_t* buf) {
>   buf->base = malloc(suggested_size);
>   buf->len = suggested_size;
> }
>
> uv_loop_t *loop;
>
> /////   Callback for after TCP stream is closed  //////////////////
> void on_close(uv_handle_t * handle)
>   {
>   printf("uv close triggered\n");
>   }
>
> // process response from central server
> void proc_central_response(uv_stream_t *central, ssize_t nread, const
> uv_buf_t *buf)
>   {
>   printf("RESPONSE from Central Control; nread=%li\n",nread);
>
> printf("---------------------------------------------------------------\n");
>   if (nread > 0) {
>     printf("buf='%s'\n",buf->base);
>
> printf("---------------------------------------------------------------\n");
>     }
>   if (nread < 0) {
>     if (nread != UV_EOF)
>           fprintf(stderr, "Read error %s\n", uv_err_name(nread));
>     free(buf->base);
>     uv_close((uv_handle_t*) central, on_close);
>     return;
>     }
>   //uv_close((uv_handle_t*) central, on_close);
>   free(buf->base);
>   return;
>   }
>
> // now connected; send http POST
> void on_central_connect(uv_connect_t* connection, int status)
>   {
>   uv_stream_t* stream;
>   uv_write_t write_req;
>   printf("Connected to Central Contro, status = %il\n",status);
>   if (status < 0) {
>      fprintf(stderr, "Error in in connecting to Central Control %s\n",
> uv_strerror(status));
>         // error!
>       return;
>     }
>   printf(" - - - - Connect to Central Control - - - ;\n");
>     // get the handle to the stream passed in connection
>   stream = connection->handle;
>
>     // make ready to receive response from Central
>   uv_read_start((uv_stream_t*) stream, alloc_buffer,
> proc_central_response);
>
>   // Now, prepare a heartbeat message to send to Central Control
>
>   struct timeval timeout;
>   char message[1024];
>   timeout.tv_sec = 10;  // if Central Host doesn't respond, we ignore
>   timeout.tv_usec = 0;  //  and will try again after the pause time
>   char tbuf[30];
>   struct timeval tv;
>   time_t curtime;
>
>   char mytoken[75];
>   strcpy(mytoken,"T12345XYZ");  // hard code this for now
>
>   gettimeofday(&tv, NULL);
>   curtime = tv.tv_sec;
>   strftime(tbuf, 30, "%m-%d-%Y-%T.",localtime(&curtime));
>   printf("Heartbeat TOD: '%s' %ld\n",tbuf,tv.tv_usec);
>   // write message to go to host
>   // message contains identity token and time stamp
>   sprintf(message,"POST /apikey/%s-%s HTTP/1.0\r\n\r\n",mytoken,tbuf);
>   uv_buf_t buf = uv_buf_init(message,strlen(message));
>   int r1 = uv_write(&write_req, stream, &buf, 1, NULL);
>   }
>
> void heartbeat(void *arg) {
>   while(1==1)
>   {
>   printf("alloc socket\n");
>   socket_central = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));\
>   printf("tcp init\n");
>   uv_tcp_init(loop, socket_central);
>   printf("alloc connection\n");
>   uv_connect_t* connect = (uv_connect_t*)malloc(sizeof(uv_connect_t));
>   uv_ip4_addr("192.168.1.67", 4000, &central_dest);
>   printf("CONNECT TO CENTRAL\n");
>   uv_tcp_connect(connect, socket_central, (const struct
> sockaddr*)&central_dest, on_central_connect);
>   printf("CONNECTION REQUEST DONE\n");
>   printf("sleep start\n");
>   sleep(5);
>   printf("sleep done\n");
>   }
> }
>
> int main() {
>   printf("start\n");
>   loop = uv_default_loop();
>
>   printf("alloc socket\n");
>   socket_central = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));\
>   printf("tcp init\n");
>   uv_tcp_init(loop, socket_central);
>
>   uv_thread_t hb_id;
>   printf("start thread\n");
>   uv_thread_create(&hb_id, heartbeat, NULL);
>   printf("past thread start\n");
>
>   uv_run(loop, UV_RUN_DEFAULT);
>
>   sleep(10);
>
>   printf("Now quitting.\n");
>
>   uv_loop_close(loop);
>   //free(loop);
>   return 0;
> }
> ```
>
> --
> You received this message because you are subscribed to the Google Groups
> "libuv" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to libuv+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/libuv/08a1297f-db66-4f6c-9682-5fb3ca3aca27o%40googlegroups.com
> <https://groups.google.com/d/msgid/libuv/08a1297f-db66-4f6c-9682-5fb3ca3aca27o%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to libuv+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/libuv/CAMLQwnnyDxAEO5bFKQ5AMP1EmL4Ls_tVTNERvjo4ZVR2M0NXgg%40mail.gmail.com.

Reply via email to