diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-21 00:16:33 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-10-21 00:24:23 +0200 |
commit | b42d245a5c408e487f132a7ee84c9f4627c5b889 (patch) | |
tree | a91762f3c841f5c14bfb4470c4671c2722948e46 /send.c | |
parent | d4e7c6876eed2733a2678668bdcabdd87659e826 (diff) |
Added minimal send implementation
Renamed minirecv.c to recv.c
Added build commands to build.sh
Added the use of common code
Diffstat (limited to 'send.c')
-rw-r--r-- | send.c | 63 |
1 files changed, 23 insertions, 40 deletions
@@ -1,10 +1,14 @@ +// minimal client implementation #include "common.h" #include <arpa/inet.h> #include <errno.h> #include <signal.h> +#include <string.h> #include <time.h> #include <unistd.h> +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; } |