diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-08-04 16:15:21 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-08-04 16:15:21 +0200 |
commit | 0004753eb1b7ebc7675df84b434f16c2b07b82f2 (patch) | |
tree | e935683d0cf76100e998ce0e5fbd67e5739b199a /examples/c-json-stdout/c-json-stdout.c | |
parent | 3fd32fb3376a32031c4bcbea38bad7482aa83215 (diff) |
added basic "consumer" example
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'examples/c-json-stdout/c-json-stdout.c')
-rw-r--r-- | examples/c-json-stdout/c-json-stdout.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/examples/c-json-stdout/c-json-stdout.c b/examples/c-json-stdout/c-json-stdout.c new file mode 100644 index 000000000..c58ef6907 --- /dev/null +++ b/examples/c-json-stdout/c-json-stdout.c @@ -0,0 +1,52 @@ +#include <arpa/inet.h> +#include <errno.h> +#include <stdio.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +#include "config.h" +#include "jsmn/jsmn.h" + +static char serv_listen_addr[INET_ADDRSTRLEN] = DISTRIBUTOR_HOST; +static uint16_t serv_listen_port = DISTRIBUTOR_PORT; + +int main(void) +{ + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in remote_addr = {}; + socklen_t remote_addrlen = sizeof(remote_addr); + uint8_t buf[BUFSIZ]; + //size_t buf_used = 0; + //unsigned long long int buf_wanted = 0; + + if (sockfd < 0) { + perror("socket"); + return 1; + } + + remote_addr.sin_family = AF_INET; + if (inet_pton(AF_INET, &serv_listen_addr[0], &remote_addr.sin_addr) != 1) { + perror("inet_pton"); + return 1; + } + remote_addr.sin_port = htons(serv_listen_port); + + if (connect(sockfd, (struct sockaddr *) &remote_addr, remote_addrlen) != 0) { + perror("connect"); + return 1; + } + + while (1) { + errno = 0; + ssize_t bytes_read = read(sockfd, buf, sizeof(buf)); + + if (bytes_read <= 0 || errno != 0) { + break; + } + + printf("RECV[%zd]: '%.*s'\n", bytes_read, (int) bytes_read, buf); + } + + return 0; +} |