From 4333fba71bbdadfe078aa7ebbe9194c6fc35af22 Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 2 Jun 2024 22:07:50 +0200 Subject: improved bytes recieved calculation --- client.c | 16 ++++++---------- listen-DGRAM.c | 5 +++-- listen.c | 10 ++++------ test/listen-DGRAM-t | Bin 32968 -> 37160 bytes 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/client.c b/client.c index 9be43ae..d0ef45f 100644 --- a/client.c +++ b/client.c @@ -17,6 +17,9 @@ #define __PORT "62000" #define BUF_SZ_2 2048 +int sockfd = -1; +size_t bytes_sent = 0; + void talk_to_server (int sockfd) { while (1) { @@ -29,19 +32,13 @@ void talk_to_server (int sockfd) { continue; } - if (editbuffer[0] == '\n') { - send(sockfd, editbuffer, fgs_len, 0); - continue; - } else { - send(sockfd, editbuffer, fgs_len, 0); - continue; - } + bytes_sent = bytes_sent + send(sockfd, editbuffer, fgs_len, 0); } } -void INThandler(int sockfd) { +void INThandler() { - fprintf(stderr, "\n"); + fprintf(stderr, "\nbytes sent: %ld\n", bytes_sent); close(sockfd); exit(0); } @@ -53,7 +50,6 @@ int main (int argc, char *argv[]) { exit(__FAIL); } - int sockfd = -1; int gai_result; int connect_result = -1; struct addrinfo hints; diff --git a/listen-DGRAM.c b/listen-DGRAM.c index 6110aa5..08f871c 100644 --- a/listen-DGRAM.c +++ b/listen-DGRAM.c @@ -16,17 +16,18 @@ #define BACKLOG 2 size_t bytes_recieved = 0; +int sockfd = -1; void INThandler() { fprintf(stderr, " SIGINT\n"); fprintf(stdout, "bytes recieved: %ld\n", bytes_recieved); + close(sockfd); 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 @@ -51,7 +52,7 @@ int main () { continue; } - bind_result = bind(sockfd, p->ai_addr, p->ai_addrlen); // Bind to socket fd. pass our own address (ai_addr) and the length + bind_result = bind(sockfd, p->ai_addr, p->ai_addrlen); if (bind_result != 0) { fprintf(stderr, "bind_result: %d\n", bind_result); close(sockfd); diff --git a/listen.c b/listen.c index 076d080..5749ea0 100644 --- a/listen.c +++ b/listen.c @@ -77,11 +77,6 @@ int main () { if (gni == 0) { fprintf(stdout, "Client %s %s just connected!\n", host_ip, port_port); } - char *msg = "Connection established!\n"; - int len; - int bytes_sent; - len = strlen(msg); - bytes_sent = send(newfd, msg, len, 0); size_t rlen = 1024; int recv_ret; @@ -99,7 +94,10 @@ int main () { //fprintf(stdout, "loop"); memset(&recvmsg, 0, rlen); } - fprintf(stdout, "bytes recieved: %ld\nbytes sent: %d\n", bytes_recieved, bytes_sent); + if (recv_ret == -1) { + bytes_recieved = bytes_recieved + 1; + } + fprintf(stdout, "bytes recieved: %ld\n", bytes_recieved); return 0; } diff --git a/test/listen-DGRAM-t b/test/listen-DGRAM-t index 5f39692..1d13ae5 100755 Binary files a/test/listen-DGRAM-t and b/test/listen-DGRAM-t differ -- cgit v1.2.3