diff options
author | Oskar <[email protected]> | 2024-06-02 15:33:57 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-06-02 15:33:57 +0200 |
commit | 8b80185d9db97a5e8e93f763a4c190183317f44f (patch) | |
tree | 7de30957d9f0458dd8df02ffe7d6299e3b895499 | |
parent | 54b7f4b1d69673182c1358d484b35e86111b66be (diff) |
made new file for dumb client, because i will make a slightly smarter one that doesnt just send a message and exit
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | client.c | 11 | ||||
-rw-r--r-- | dumbclient.c | 67 |
3 files changed, 83 insertions, 4 deletions
@@ -7,21 +7,24 @@ TARGETS2=binding TARGETS3=listen TARGETS4=connecting TARGETS5=client +TARGETS6=dumbclient TESTTARGET=helloworld-t TESTTARGET2=binding-t TESTTARGET3=listen-t TESTTARGET4=connecting-t TESTTARGET5=client-t +TESTTARGET6=dumbclient-t SRCS=hello.c SRCS2=binding.c SRCS3=listen.c SRCS4=connecting.c SRCS5=client.c +SRCS6=dumbclient.c all: release clean: - rm -f bin/$(TARGETS) bin/$(TARGETS2) bin/$(TARGETS3) bin/$(TARGETS4) bin/$(TARGETS5) - rm -f test/$(TESTTARGET) test/$(TESTTARGET2) test/$(TESTTARGET3) test/$(TESTTARGET4) test/$(TESTTARGET5) + rm -f bin/$(TARGETS) bin/$(TARGETS2) bin/$(TARGETS3) bin/$(TARGETS4) bin/$(TARGETS5) bin/$(TARGETS6) + rm -f test/$(TESTTARGET) test/$(TESTTARGET2) test/$(TESTTARGET3) test/$(TESTTARGET4) test/$(TESTTARGET5) test/$(TESTTARGET6) tests: $(CC) $(CFLAGS_TESTBIN) $(SRCS) -o test/$(TESTTARGET) @@ -29,6 +32,7 @@ tests: $(CC) $(CFLAGS_TESTBIN) $(SRCS3) -o test/$(TESTTARGET3) $(CC) $(CFLAGS_TESTBIN) $(SRCS4) -o test/$(TESTTARGET4) $(CC) $(CFLAGS_TESTBIN) $(SRCS5) -o test/$(TESTTARGET5) + $(CC) $(CFLAGS_TESTBIN) $(SRCS6) -o test/$(TESTTARGET6) release: $(CC) $(CFLAGS) $(SRCS) -o bin/$(TARGETS) @@ -36,3 +40,4 @@ release: $(CC) $(CFLAGS) $(SRCS3) -o bin/$(TARGETS3) $(CC) $(CFLAGS) $(SRCS4) -o bin/$(TARGETS4) $(CC) $(CFLAGS) $(SRCS5) -o bin/$(TARGETS5) + $(CC) $(CFLAGS) $(SRCS6) -o bin/$(TARGETS6) @@ -50,11 +50,18 @@ int main () { } } - if (connect_result == -1 || sockfd == -1) { exit(__FAIL); } + if (connect_result == -1 || sockfd == -1) { + exit(__FAIL); + } + char *msg = "Client says hello!\n"; size_t len = strlen(msg); - send(sockfd, msg, len, 0); + + for (int i = 0 ; i < 10 ; i++) { + + send(sockfd, msg, len, 0); + } freeaddrinfo(res); return 0; } diff --git a/dumbclient.c b/dumbclient.c new file mode 100644 index 0000000..e023465 --- /dev/null +++ b/dumbclient.c @@ -0,0 +1,67 @@ +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include <unistd.h> +#include <stdbool.h> +#include <fcntl.h> +#include <stdlib.h> +#include <arpa/inet.h> +#include <netinet/in.h> + +// int listen(int sockfd, int backlog); +#define __FAIL EXIT_FAILURE +#define BACKLOG 2 +#define __PORT "62000" + +int main () { + + int sockfd = -1; + int gai_result; + int connect_result = -1; + struct addrinfo hints; + struct addrinfo *res; + struct addrinfo *p; + memset(&hints, 0, sizeof(hints)); + //hints.ai_flags = ; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + gai_result = getaddrinfo("192.168.1.18", __PORT, &hints, &res); + if (gai_result != 0) { + fprintf(stderr, "getaddrinfo: %d", gai_result); + exit(__FAIL); + } + + for (p = res ; p != NULL ; p = p->ai_next) { + + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sockfd == -1) { + fprintf(stderr, "socket fail\n"); + continue; + } + + connect_result = connect(sockfd, p->ai_addr, p->ai_addrlen); + if (connect_result == -1) { + fprintf(stderr, "connect fail\n"); + continue; + } + + } + if (connect_result == -1 || sockfd == -1) { + exit(__FAIL); + } + + + char *msg = "Client says hello!\n"; + size_t len = strlen(msg); + + for (int i = 0 ; i < 10 ; i++) { + + send(sockfd, msg, len, 0); + } + freeaddrinfo(res); + return 0; +} |