summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-06-03 10:46:16 +0200
committerOskar <[email protected]>2024-06-03 10:46:16 +0200
commitcf86eea3ef47ce4444b99edff866a7d9836bd763 (patch)
treeb4807c241595755035557b44001bc053e513917c
parent4333fba71bbdadfe078aa7ebbe9194c6fc35af22 (diff)
udp client-DGRAM
-rw-r--r--Makefile9
-rw-r--r--binding.c2
-rw-r--r--client-DGRAM.c91
-rw-r--r--connecting.c2
-rw-r--r--dumbclient.c2
-rw-r--r--listen-DGRAM.c2
-rw-r--r--listen.c2
-rwxr-xr-xtest/listen-DGRAM-tbin37160 -> 0 bytes
8 files changed, 103 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 95db456..7fa7a01 100644
--- a/Makefile
+++ b/Makefile
@@ -9,6 +9,7 @@ TARGETS4=connecting
TARGETS5=client
TARGETS6=dumbclient
TARGETS7=listen-DGRAM
+TARGETS8=client-DGRAM
TESTTARGET=helloworld-t
TESTTARGET2=binding-t
@@ -17,6 +18,7 @@ TESTTARGET4=connecting-t
TESTTARGET5=client-t
TESTTARGET6=dumbclient-t
TESTTARGET7=listen-DGRAM-t
+TESTTARGET8=client-DGRAM-t
SRCS=hello.c
SRCS2=binding.c
@@ -25,11 +27,12 @@ SRCS4=connecting.c
SRCS5=client.c
SRCS6=dumbclient.c
SRCS7=listen-DGRAM.c
+SRCS8=client-DGRAM.c
all: release
clean:
- 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)
+ rm -f bin/$(TARGETS) bin/$(TARGETS2) bin/$(TARGETS3) bin/$(TARGETS4) bin/$(TARGETS5) bin/$(TARGETS6) bin/$(TARGETS7) bin/$(TARGETS8)
+ rm -f test/$(TESTTARGET) test/$(TESTTARGET2) test/$(TESTTARGET3) test/$(TESTTARGET4) test/$(TESTTARGET5) test/$(TESTTARGET6) test/$(TESTTARGET7) test/$(TESTTARGET8)
tests:
$(CC) $(CFLAGS_TESTBIN) $(SRCS) -o test/$(TESTTARGET)
@@ -39,6 +42,7 @@ tests:
$(CC) $(CFLAGS_TESTBIN) $(SRCS5) -o test/$(TESTTARGET5)
$(CC) $(CFLAGS_TESTBIN) $(SRCS6) -o test/$(TESTTARGET6)
$(CC) $(CFLAGS_TESTBIN) $(SRCS7) -o test/$(TESTTARGET7)
+ $(CC) $(CFLAGS_TESTBIN) $(SRCS8) -o test/$(TESTTARGET8)
release:
$(CC) $(CFLAGS) $(SRCS) -o bin/$(TARGETS)
@@ -48,3 +52,4 @@ release:
$(CC) $(CFLAGS) $(SRCS5) -o bin/$(TARGETS5)
$(CC) $(CFLAGS) $(SRCS6) -o bin/$(TARGETS6)
$(CC) $(CFLAGS) $(SRCS7) -o bin/$(TARGETS7)
+ $(CC) $(CFLAGS) $(SRCS8) -o bin/$(TARGETS8)
diff --git a/binding.c b/binding.c
index ebc503d..d72dc03 100644
--- a/binding.c
+++ b/binding.c
@@ -15,7 +15,7 @@
// Bind section of beej networking
-int main () {
+int main (void) {
struct addrinfo hints;
struct addrinfo *res;
diff --git a/client-DGRAM.c b/client-DGRAM.c
new file mode 100644
index 0000000..f447d37
--- /dev/null
+++ b/client-DGRAM.c
@@ -0,0 +1,91 @@
+#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
+#define __PORT "62000"
+#define BUF_SZ_2 2048
+
+int sockfd = -1;
+size_t bytes_sent = 0;
+struct addrinfo *resfreer;
+struct addrinfo *pglobal;
+
+void talk_to_server (int sockfd) {
+
+ while (1) {
+ char editbuffer[BUF_SZ_2];
+ fprintf(stdout, ": ");
+
+ char *fgs = fgets(editbuffer, BUF_SZ_2, stdin);
+ socklen_t fgs_len = strlen(editbuffer);
+ if (fgs == NULL) {
+ continue;
+ }
+
+ bytes_sent = bytes_sent + sendto(sockfd, editbuffer, fgs_len, 0, pglobal->ai_addr, pglobal->ai_addrlen);
+ }
+}
+
+void INThandler() {
+
+ fprintf(stderr, "\nbytes sent: %ld\n", bytes_sent);
+ freeaddrinfo(resfreer);
+ close(sockfd);
+ exit(0);
+}
+
+int main (int argc, char *argv[]) {
+
+ if (argc != 2) {
+ fprintf(stderr, "USAGE: %s [IP]\n", argv[0]);
+ exit(__FAIL);
+ }
+
+ int gai_result;
+ struct addrinfo hints;
+ struct addrinfo *res;
+ struct addrinfo *p;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ gai_result = getaddrinfo(argv[1], __PORT, &hints, &res);
+ if (gai_result != 0) {
+ fprintf(stderr, "getaddrinfo: %d\n", 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;
+ } else {
+ pglobal = p;
+ }
+
+ }
+ if (sockfd == -1) {
+ close(sockfd);
+ exit(__FAIL);
+ }
+
+ signal(SIGINT, INThandler);
+ resfreer = res;
+ talk_to_server(sockfd);
+
+ return 0;
+}
diff --git a/connecting.c b/connecting.c
index fe9b99e..40da78e 100644
--- a/connecting.c
+++ b/connecting.c
@@ -11,7 +11,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
-int main () {
+int main (void) {
int sockfd;
struct addrinfo *res;
diff --git a/dumbclient.c b/dumbclient.c
index e023465..505e93e 100644
--- a/dumbclient.c
+++ b/dumbclient.c
@@ -16,7 +16,7 @@
#define BACKLOG 2
#define __PORT "62000"
-int main () {
+int main (void) {
int sockfd = -1;
int gai_result;
diff --git a/listen-DGRAM.c b/listen-DGRAM.c
index 08f871c..feecd63 100644
--- a/listen-DGRAM.c
+++ b/listen-DGRAM.c
@@ -26,7 +26,7 @@ void INThandler() {
exit(0);
}
-int main () {
+int main (void) {
int gai_result; // result from getaddrinfo
int bind_result = -1; // result from bind
diff --git a/listen.c b/listen.c
index 5749ea0..f85c2c2 100644
--- a/listen.c
+++ b/listen.c
@@ -15,7 +15,7 @@
#define __FAIL EXIT_FAILURE
#define BACKLOG 2
-int main () {
+int main (void) {
int sockfd = -1; // socket fd
int gai_result; // result from getaddrinfo
diff --git a/test/listen-DGRAM-t b/test/listen-DGRAM-t
deleted file mode 100755
index 1d13ae5..0000000
--- a/test/listen-DGRAM-t
+++ /dev/null
Binary files differ