diff options
author | Oskar <[email protected]> | 2024-06-02 21:32:41 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-06-02 21:32:41 +0200 |
commit | 4d60b8da9cdcd4716fe65c8872ec60a14d2c1e48 (patch) | |
tree | 25d7a1e11a1211364bbf8be279a2d05c5251755a | |
parent | 7a4372aace921fa78bfab14a450867f535d557b2 (diff) |
listen-DGRAM which is basically a version of listen but with UDP
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | client.c | 17 | ||||
-rw-r--r-- | listen-DGRAM.c | 90 | ||||
-rwxr-xr-x | test/listen-DGRAM-t | bin | 0 -> 32968 bytes |
4 files changed, 111 insertions, 7 deletions
@@ -8,23 +8,28 @@ TARGETS3=listen TARGETS4=connecting TARGETS5=client TARGETS6=dumbclient +TARGETS7=listen-DGRAM + TESTTARGET=helloworld-t TESTTARGET2=binding-t TESTTARGET3=listen-t TESTTARGET4=connecting-t TESTTARGET5=client-t TESTTARGET6=dumbclient-t +TESTTARGET7=listen-DGRAM-t + SRCS=hello.c SRCS2=binding.c SRCS3=listen.c SRCS4=connecting.c SRCS5=client.c SRCS6=dumbclient.c +SRCS7=listen-DGRAM.c all: release clean: - 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) + rm -f bin/$(TARGETS) bin/$(TARGETS2) bin/$(TARGETS3) bin/$(TARGETS4) bin/$(TARGETS5) bin/$(TARGETS6) bin/$(TARGETS7) + rm -f test/$(TESTTARGET) test/$(TESTTARGET2) test/$(TESTTARGET3) test/$(TESTTARGET4) test/$(TESTTARGET5) test/$(TESTTARGET6) test/$(TESTTARGET6) tests: $(CC) $(CFLAGS_TESTBIN) $(SRCS) -o test/$(TESTTARGET) @@ -33,6 +38,7 @@ tests: $(CC) $(CFLAGS_TESTBIN) $(SRCS4) -o test/$(TESTTARGET4) $(CC) $(CFLAGS_TESTBIN) $(SRCS5) -o test/$(TESTTARGET5) $(CC) $(CFLAGS_TESTBIN) $(SRCS6) -o test/$(TESTTARGET6) + $(CC) $(CFLAGS_TESTBIN) $(SRCS7) -o test/$(TESTTARGET7) release: $(CC) $(CFLAGS) $(SRCS) -o bin/$(TARGETS) @@ -41,3 +47,4 @@ release: $(CC) $(CFLAGS) $(SRCS4) -o bin/$(TARGETS4) $(CC) $(CFLAGS) $(SRCS5) -o bin/$(TARGETS5) $(CC) $(CFLAGS) $(SRCS6) -o bin/$(TARGETS6) + $(CC) $(CFLAGS) $(SRCS7) -o bin/$(TARGETS7) @@ -10,14 +10,14 @@ #include <stdlib.h> #include <arpa/inet.h> #include <netinet/in.h> +#include <signal.h> -// int listen(int sockfd, int backlog); #define __FAIL EXIT_FAILURE #define BACKLOG 2 #define __PORT "62000" #define BUF_SZ_2 2048 -int talk_to_server (int sockfd) { +void talk_to_server (int sockfd) { while (1) { char editbuffer[BUF_SZ_2]; @@ -26,7 +26,7 @@ int talk_to_server (int sockfd) { char *fgs = fgets(editbuffer, BUF_SZ_2, stdin); socklen_t fgs_len = strlen(editbuffer); if (fgs == NULL) { - return 0; + continue; } if (editbuffer[0] == '\n') { @@ -37,7 +37,13 @@ int talk_to_server (int sockfd) { continue; } } - return 0; +} + +void INThandler(int sockfd) { + + fprintf(stderr, "\n"); + close(sockfd); + exit(0); } int main (int argc, char *argv[]) { @@ -54,7 +60,6 @@ int main (int argc, char *argv[]) { struct addrinfo *res; struct addrinfo *p; memset(&hints, 0, sizeof(hints)); - //hints.ai_flags = ; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; @@ -80,9 +85,11 @@ int main (int argc, char *argv[]) { } if (connect_result == -1 || sockfd == -1) { + close(sockfd); exit(__FAIL); } + signal(SIGINT, INThandler); freeaddrinfo(res); talk_to_server(sockfd); diff --git a/listen-DGRAM.c b/listen-DGRAM.c new file mode 100644 index 0000000..6110aa5 --- /dev/null +++ b/listen-DGRAM.c @@ -0,0 +1,90 @@ +#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> +#include <signal.h> + +#define __FAIL EXIT_FAILURE +#define BACKLOG 2 + +size_t bytes_recieved = 0; + +void INThandler() { + + fprintf(stderr, " SIGINT\n"); + fprintf(stdout, "bytes recieved: %ld\n", bytes_recieved); + exit(0); +} + +int main () { + + int sockfd = -1; // socket fd + int gai_result; // result from getaddrinfo + int bind_result = -1; // result from bind + struct addrinfo *res; // Pointer to linked list sent back by gai + struct addrinfo *p; // for linked list stuff + struct addrinfo hints; // Options we pass to gai + memset(&hints, 0, sizeof(hints)); // We set this struct to 0 and then give it all the options we want below + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = AI_PASSIVE; + + gai_result = getaddrinfo(NULL, "62000", &hints, &res); // NULL because we are going to listen + if (gai_result != 0) { + fprintf(stderr, "gai_result: %d\n", gai_result); + exit(__FAIL); + } + + for (p = res ; p != NULL ; p = p->ai_next) { // Loop + + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); // Socket, get the family, socktype and protocol. + if (sockfd == -1) { + fprintf(stderr, "sockfd: %d\n", sockfd); + continue; + } + + bind_result = bind(sockfd, p->ai_addr, p->ai_addrlen); // Bind to socket fd. pass our own address (ai_addr) and the length + if (bind_result != 0) { + fprintf(stderr, "bind_result: %d\n", bind_result); + close(sockfd); + continue; + } + } + if (sockfd == -1 || bind_result == -1) { + exit(__FAIL); + } + + freeaddrinfo(res); // free the info because we dont need it anymore + + fprintf(stdout, "Listening...\n"); + + struct sockaddr_storage from; + socklen_t fromlen = sizeof(struct sockaddr_storage); + size_t rlen = 1024; + int recv_ret; + + signal(SIGINT, INThandler); + while(true) { + + char recvmsg[rlen]; + recv_ret = recvfrom(sockfd, recvmsg, rlen, 0, (struct sockaddr *)&from, &fromlen); + bytes_recieved = bytes_recieved+recv_ret; + if (recv_ret < 1) { + break; + } + recvmsg[recv_ret] = '\0'; + fprintf(stdout, "%s", recvmsg); + //fprintf(stdout, "loop"); + memset(&recvmsg, 0, rlen); + } + + return 0; +} diff --git a/test/listen-DGRAM-t b/test/listen-DGRAM-t Binary files differnew file mode 100755 index 0000000..5f39692 --- /dev/null +++ b/test/listen-DGRAM-t |