diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2026-07-17 15:24:11 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2026-07-17 15:24:11 +0300 |
| commit | a2924e66d09c1f45892859f0f1810319d67df7a2 (patch) | |
| tree | 513bf030c975a2b0b520a2e61a7f03f25cdf9cbe /src/client.c | |
| download | lark-a2924e66d09c1f45892859f0f1810319d67df7a2.tar.gz lark-a2924e66d09c1f45892859f0f1810319d67df7a2.zip | |
initial echo server arch
Diffstat (limited to 'src/client.c')
| -rw-r--r-- | src/client.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/client.c b/src/client.c new file mode 100644 index 0000000..3d95287 --- /dev/null +++ b/src/client.c @@ -0,0 +1,64 @@ +#include <err.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <netdb.h> + +int main() +{ + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = 0; + hints.ai_protocol = 0; + + struct addrinfo *result; + int ret = getaddrinfo("127.0.0.1", "24242", &hints, &result); + if (ret == -1) { + err(EXIT_FAILURE, "failed getting addr info"); + } + + int tcp_sock = -1; + + for (struct addrinfo *rp = result; rp != NULL; rp = rp->ai_next) { + int s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (s == -1) + continue; + + if (connect(s, rp->ai_addr, rp->ai_addrlen) != -1) { + tcp_sock = s; + break; + } + + close(s); + } + + freeaddrinfo(result); + if (tcp_sock == -1) { + err(EXIT_FAILURE, "couldn't connect to socket"); + return -1; + } + + int c; + while ((c = fgetc(stdin)) != EOF) { + char cb = c; + int ret = write(tcp_sock, &cb, 1); + if (ret == -1) { + err(EXIT_FAILURE, "failed writing to socket"); + } + + ret = read(tcp_sock, &cb, 1); + if (ret == -1) { + err(EXIT_FAILURE, "failed reading from socket"); + } + + fprintf(stdout, "%c", cb); + } + + close(tcp_sock); +} |
