From b42d245a5c408e487f132a7ee84c9f4627c5b889 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Mon, 21 Oct 2024 00:16:33 +0200 Subject: Added minimal send implementation Renamed minirecv.c to recv.c Added build commands to build.sh Added the use of common code --- .gitignore | 2 ++ README.md | 1 + build.sh | 2 ++ minirecv.c | 67 -------------------------------------------------------------- recv.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ send.c | 63 +++++++++++++++++++++------------------------------------- 6 files changed, 85 insertions(+), 107 deletions(-) delete mode 100644 minirecv.c create mode 100644 recv.c diff --git a/.gitignore b/.gitignore index 79557cf..fd52ccf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ chatty server +recv +send tags tmp diff --git a/README.md b/README.md index 2b02f02..2eb012e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Chatty - use memory arena's to manage memory +- spec for the stream protocol # Server The idea is the following: diff --git a/build.sh b/build.sh index 60a538d..bfd8c19 100755 --- a/build.sh +++ b/build.sh @@ -2,3 +2,5 @@ set -x gcc -g -Wall -pedantic -std=c99 -o chatty client.c common.c gcc -g -Wall -pedantic -std=c99 -o server server.c common.c +gcc -g -Wall -pedantic -std=c99 -o send send.c common.c +gcc -g -Wall -pedantic -std=c99 -o recv recv.c common.c diff --git a/minirecv.c b/minirecv.c deleted file mode 100644 index bba373a..0000000 --- a/minirecv.c +++ /dev/null @@ -1,67 +0,0 @@ -#include "common.h" -#include -#include -#include -#include -#include - -int main(void) -{ - int serverfd, clientfd; - int on = 1; - - const struct sockaddr_in address = { - AF_INET, - htons(9999), - {0}, - }; - - serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); - if (bind(serverfd, (struct sockaddr *)&address, sizeof(address))) - return 1; - - listen(serverfd, 256); - - writef("serverfd: %d\n", serverfd); - clientfd = accept(serverfd, 0, 0); - writef("clientfd: %d\n", clientfd); - - struct pollfd fds[1] = { - {clientfd, POLLIN, 0}, - }; - - for (;;) { - int ret = poll(fds, 1, 50000); - if (ret == -1) - return 2; - - if (fds[0].revents & POLLIN) { - int nrecv; - - char buf[20]; - - nrecv = recv(clientfd, buf, sizeof(buf), 0); - printf("received %d bytes\n", nrecv); - nrecv = recv(clientfd, buf, sizeof(buf), 0); - printf("received %d bytes\n", nrecv); - nrecv = recv(clientfd, buf, sizeof(buf), 0); - printf("received %d bytes\n", nrecv); - - return 3; - - if (nrecv == -1) { - return errno; - } else if (nrecv == 0) { - writef("Disconnect.\n"); - fds[0].fd = -1; - fds[0].revents = 0; - close(clientfd); - } - - writef("received: %d bytes\n", nrecv); - } - } - - return 0; -} diff --git a/recv.c b/recv.c new file mode 100644 index 0000000..e4433ae --- /dev/null +++ b/recv.c @@ -0,0 +1,57 @@ +// Minimal server implementation for probing out things + +#include "common.h" +#include +#include +#include +#include +#include + +int main(void) +{ + u32 serverfd, clientfd; + u8 on = 1; + + const struct sockaddr_in address = { + AF_INET, + htons(PORT), + {0}, + }; + + serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); + if (bind(serverfd, (struct sockaddr *)&address, sizeof(address))) + return 1; + + listen(serverfd, 256); + + clientfd = accept(serverfd, 0, 0); + + struct pollfd fds[1] = { + {clientfd, POLLIN, 0}, + }; + + for (;;) { + int ret = poll(fds, 1, 50000); + if (ret == -1) + return 2; + + if (fds[0].revents & POLLIN) { + u8 recv_buf[BUF_MAX]; + u32 nrecv = recv(clientfd, recv_buf, sizeof(recv_buf), 0); + + writef("client(%d): %d bytes received.\n", clientfd, nrecv); + if (nrecv == -1) { + return errno; + } else if (nrecv == 0) { + writef("client(%d): disconnected.\n", clientfd); + fds[0].fd = -1; + fds[0].revents = 0; + close(clientfd); + return 0; + } + } + } + + return 0; +} diff --git a/send.c b/send.c index 50ab373..693321e 100644 --- a/send.c +++ b/send.c @@ -1,10 +1,14 @@ +// minimal client implementation #include "common.h" #include #include #include +#include #include #include +u32 serverfd; + // NOTE: Errno could be unset and contain an error for a previous command void debug_panic(const char *msg) { @@ -12,65 +16,44 @@ void debug_panic(const char *msg) raise(SIGINT); } -int main(void) +// get current time in timestamp string +void timestamp(char timestamp[MESSAGE_TIMESTAMP_LEN]) { - // time for a new entered message time_t now; - // localtime of new sent message struct tm *ltime; + time(&now); + ltime = localtime(&now); + strftime(timestamp, MESSAGE_TIMESTAMP_LEN, "%H:%M:%S", ltime); +} - int serverfd; - struct message input = { - .author = "Friendship", - }; +int main(void) +{ serverfd = socket(AF_INET, SOCK_STREAM, 0); if (serverfd == -1) debug_panic("Error while getting socket."); - // Set timestamp for the message - time(&now); - ltime = localtime(&now); - strftime(input.timestamp, sizeof(input.timestamp), "%H:%M:%S", ltime); - - input.text = "HII!!"; - input.len = str_len(input.text); - const struct sockaddr_in address = { AF_INET, - htons(9999), + htons(PORT), {0}, }; if (connect(serverfd, (struct sockaddr *)&address, sizeof(address))) debug_panic("Error while connecting."); - u16 buf_len = MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + input.len; - printf("buf_len: %d\n", buf_len); - char buf[buf_len]; - str_cpy(buf, input.author); - str_cpy(buf + MESSAGE_AUTHOR_LEN, input.timestamp); - str_cpy(buf + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN, input.text); - - int n = send(serverfd, &buf, buf_len, 0); - if (n == -1) - debug_panic("Error while sending message"); - writef("%d bytes sent.\n", n); + struct message input = { + .author = "Friendship", + .timestamp = "" + }; + input.text = "HII!!"; + input.len = str_len(input.text); + timestamp(input.timestamp); - { - input.text = "cleared"; - u16 buf_len = MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + input.len; - printf("buf_len: %d\n", buf_len); - char buf[buf_len]; - str_cpy(buf, input.author); - str_cpy(buf + MESSAGE_AUTHOR_LEN, input.timestamp); - str_cpy(buf + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN, input.text); - int n = send(serverfd, &buf, buf_len, 0); - if (n == -1) - debug_panic("Error while sending message"); - writef("%d bytes sent.\n", n); - } + send_message(input, serverfd); + // send_message(input); + // send_message(input); return 0; } -- cgit v1.2.3