aboutsummaryrefslogtreecommitdiff
path: root/send.c
diff options
context:
space:
mode:
Diffstat (limited to 'send.c')
-rw-r--r--send.c63
1 files changed, 23 insertions, 40 deletions
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 <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;
}