From 4fe82b42b6f6fb1a69da2fb2823831e6b3eaa036 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Sun, 20 Oct 2024 19:40:09 +0200 Subject: Added mvp implementations of the server and client for testing --- send.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 send.c (limited to 'send.c') diff --git a/send.c b/send.c new file mode 100644 index 0000000..50ab373 --- /dev/null +++ b/send.c @@ -0,0 +1,76 @@ +#include "common.h" +#include +#include +#include +#include +#include + +// NOTE: Errno could be unset and contain an error for a previous command +void debug_panic(const char *msg) +{ + writef("%s errno: %d\n", msg, errno); + raise(SIGINT); +} + +int main(void) +{ + // time for a new entered message + time_t now; + // localtime of new sent message + struct tm *ltime; + + int serverfd; + + struct message input = { + .author = "Friendship", + }; + + 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), + {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); + + { + 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); + } + + return 0; +} -- cgit v1.2.3