diff options
author | Oskar <[email protected]> | 2024-06-03 17:33:47 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-06-03 17:33:47 +0200 |
commit | 9d15452d00945770b8f297daed2e5f030f419202 (patch) | |
tree | af1b2264c9231e9eadbe0c18906ad08a8d79bc7b | |
parent | cf86eea3ef47ce4444b99edff866a7d9836bd763 (diff) |
made it so the client can keep accepting new connections
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | client.c | 8 | ||||
-rw-r--r-- | listen.c | 93 | ||||
-rw-r--r-- | test/Makefile | 4 |
4 files changed, 63 insertions, 45 deletions
@@ -1,6 +1,7 @@ CC=gcc CFLAGS_TESTBIN=-Og -fsanitize=address -Wfatal-errors -Wall -Werror -Werror=return-type -Wextra -g -Wpedantic -std=gnu99 CFLAGS=-O3 -flto -march=native -DNDEBUG -fomit-frame-pointer -std=gnu99 +CFLAGS_BEEJ_TESTBIN=-Og -fsanitize=address -g -std=gnu99 MAKEFLAGS += -j$(nproc) TARGETS=helloworld TARGETS2=binding @@ -32,7 +33,7 @@ SRCS8=client-DGRAM.c all: release clean: 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) + 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) @@ -21,7 +21,13 @@ int sockfd = -1; size_t bytes_sent = 0; void talk_to_server (int sockfd) { - + + fprintf(stdout, "Waiting to connect...\n"); + int rcv_ret; + char buf_connected[11]; + rcv_ret = recv(sockfd, buf_connected, 11, 0); + buf_connected[rcv_ret] = '\0'; + fprintf(stdout, "%s", buf_connected); while (1) { char editbuffer[BUF_SZ_2]; fprintf(stdout, ": "); @@ -13,10 +13,36 @@ // int listen(int sockfd, int backlog); #define __FAIL EXIT_FAILURE -#define BACKLOG 2 +#define BACKLOG 128 -int main (void) { +size_t recv_message_loop (int newfd) { + size_t rlen = 1024; + int recv_ret; + size_t bytes_recieved = 0; + while(true) { + + char recvmsg[rlen]; + recv_ret = recv(newfd, recvmsg, rlen, 0); + 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); + } + if (recv_ret == -1) { + bytes_recieved = bytes_recieved + 1; + } + fprintf(stdout, "bytes recieved: %ld\n", bytes_recieved); + + return bytes_recieved; +} + +int main (void) { + int sockfd = -1; // socket fd int gai_result; // result from getaddrinfo int bind_result = -1; // result from bind @@ -55,49 +81,34 @@ int main (void) { freeaddrinfo(res); // free the info because we dont need it anymore - fprintf(stdout, "Waiting for a connection...\n"); listen(sockfd, BACKLOG); // Actually listen for connections with the sockfd - - socklen_t addr_size; // Size of address - struct sockaddr_storage their_addr; // pass this struct to accept because it will hold their address - addr_size = sizeof(their_addr); // Check the size of their addr - int newfd; // New fd - newfd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size); // Actually accept. Pass the sockfd, cast their_addr to sockaddr and send the address to the function. Pass the addr_size address so we can handle the length. - fprintf(stdout, "Connection is being established...\n"); - if (newfd == -1) { - fprintf(stderr, "newfd: %d\n", newfd); - exit(__FAIL); - } - char host_ip[NI_MAXHOST]; - char port_port[NI_MAXSERV]; - int gni; - gni = getnameinfo((struct sockaddr *)&their_addr, addr_size, host_ip, sizeof(host_ip), port_port, sizeof(port_port), NI_NUMERICSERV | NI_NUMERICHOST); - - if (gni == 0) { - fprintf(stdout, "Client %s %s just connected!\n", host_ip, port_port); - } - - size_t rlen = 1024; - int recv_ret; - size_t bytes_recieved = 0; - while(true) { + while (1) { + fprintf(stdout, "Waiting for a connection...\n"); + int newfd; + socklen_t addr_size; // Size of address + struct sockaddr_storage their_addr; // pass this struct to accept because it will hold their address + addr_size = sizeof(their_addr); // Check the size of their addr + newfd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size); // Actually accept. Pass the sockfd, cast their_addr to sockaddr and send the address to the function. Pass the addr_size address so we can handle the length. + fprintf(stdout, "Connection is being established...\n"); + if (newfd == -1) { + fprintf(stderr, "newfd: %d\n", newfd); + exit(__FAIL); + } - char recvmsg[rlen]; - recv_ret = recv(newfd, recvmsg, rlen, 0); - bytes_recieved = bytes_recieved+recv_ret; - if (recv_ret < 1) { - break; + char host_ip[NI_MAXHOST]; + char port_port[NI_MAXSERV]; + int gni; + gni = getnameinfo((struct sockaddr *)&their_addr, addr_size, host_ip, sizeof(host_ip), port_port, sizeof(port_port), NI_NUMERICSERV | NI_NUMERICHOST); + + if (gni == 0) { + fprintf(stdout, "Client %s %s just connected!\n", host_ip, port_port); } - recvmsg[recv_ret] = '\0'; - fprintf(stdout, "%s", recvmsg); - //fprintf(stdout, "loop"); - memset(&recvmsg, 0, rlen); - } - if (recv_ret == -1) { - bytes_recieved = bytes_recieved + 1; + + char *msg = "Connected\n"; + send(newfd, msg, 10, 0); + recv_message_loop(newfd); } - fprintf(stdout, "bytes recieved: %ld\n", bytes_recieved); - + return 0; } diff --git a/test/Makefile b/test/Makefile index 48a5814..6af8165 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ all: release clean: cd .. && make clean - + release: - cd .. && make tests + cd .. && make tests && make beej |