From f37cce374cc9da243fc2babbea9a7051f15db80a Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Thu, 24 Oct 2024 23:10:31 +0200 Subject: Archive code into v1/ directory The code was written too fast and I deemed that it would be better to make a new draft. --- v1/send.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 v1/send.c (limited to 'v1/send.c') diff --git a/v1/send.c b/v1/send.c new file mode 100644 index 0000000..27e9793 --- /dev/null +++ b/v1/send.c @@ -0,0 +1,61 @@ +// minimal client implementation +#include "common.h" +#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) +{ + writef("%s errno: %d\n", msg, errno); + raise(SIGINT); +} + +// get current time in timestamp string +void timestamp(u8 timestamp[MESSAGE_TIMESTAMP_LEN]) +{ + time_t now; + struct tm *ltime; + time(&now); + ltime = localtime(&now); + strftime((char*)timestamp, MESSAGE_TIMESTAMP_LEN, "%H:%M:%S", ltime); +} + +int main(int argc, char **argv) +{ + + if (argc < 2) { + printf("usage: send \n"); + return 1; + } + + Message input = { + .author = "Friendship", + }; + input.text = argv[1]; + input.text_len = str_len(input.text); + + serverfd = socket(AF_INET, SOCK_STREAM, 0); + if (serverfd == -1) + debug_panic("Error while getting socket."); + + const struct sockaddr_in address = { + AF_INET, + htons(PORT), + {0}, + }; + + if (connect(serverfd, (struct sockaddr *)&address, sizeof(address))) + debug_panic("Error while connecting."); + + printf("input.len: %d\n", input.text_len); + timestamp(input.timestamp); + + message_send(&input, serverfd); + + return 0; +} -- cgit v1.2.3