aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-21 12:53:03 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-21 12:54:31 +0200
commit65d137348f400d37fb95b4d4076de145f5a22bff (patch)
treeb2738de9e6c04a9576066f721b46a9ba985e6aee
parentc616e09df8783a4f850f38f0b79b5029e781f932 (diff)
Moved common.c to common.h
- fixed bug(server.c): `on` not being 32 bytes - cleanup
-rw-r--r--common.c135
-rw-r--r--common.h132
-rw-r--r--send.c8
-rw-r--r--server.c2
4 files changed, 134 insertions, 143 deletions
diff --git a/common.c b/common.c
deleted file mode 100644
index 34bed33..0000000
--- a/common.c
+++ /dev/null
@@ -1,135 +0,0 @@
-#include "common.h"
-
-#include <stdarg.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/socket.h>
-
-
-void writef(char *format, ...)
-{
- char buf[255 + 1];
- va_list args;
- va_start(args, format);
-
- vsnprintf(buf, sizeof(buf), format, args);
- va_end(args);
-
- int n = 0;
- while (*(buf + n) != 0)
- n++;
- write(0, buf, n);
-}
-
-u16 str_len(char *str)
-{
- u16 i = 0;
- while (str[i])
- i++;
- return i;
-}
-
-void str_cpy(char *to, char *from)
-{
- while ((*to++ = *from++))
- ;
-}
-
-u8 save_message(struct message *msg, FILE *f)
-{
- u8 err = 0;
- u16 len;
- if (msg->text == NULL) {
- len = 0;
- msg->text = ""; // TODO: Error empty message should not be allowed.
- } else {
- len = str_len(msg->text);
- }
-
- if (len == 0)
- err = 1;
-
- fwrite(&msg->timestamp, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN, 1, f);
- fwrite(&msg->author, sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
- fwrite(&len, sizeof(len), 1, f);
- fputs(msg->text, f);
-
- return err;
-}
-
-u8 load_message(struct message *msg, FILE *f)
-{
- fread(msg, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN + sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
- u16 len;
- fread(&len, sizeof(len), 1, f);
- if (len == 0) {
- // TODO: Error: empty message should not be allowed
- // empty message
- msg->text = "";
- return 1;
- }
- char txt[len];
- fgets(txt, len, f);
- msg->text = txt;
-
- return 0;
-}
-
-u32 send_message(struct message msg, u32 serverfd)
-{
- // stream length : message author : message timestamp : message text
- u32 buf_len = sizeof(buf_len) + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + msg.len;
- char buf[buf_len];
- u32 offset;
-
- memcpy(buf, &buf_len, sizeof(buf_len));
- offset = sizeof(buf_len);
- memcpy(buf + offset, msg.author, MESSAGE_AUTHOR_LEN);
- offset += MESSAGE_AUTHOR_LEN;
- memcpy(buf + offset, msg.timestamp, MESSAGE_TIMESTAMP_LEN);
- offset += MESSAGE_TIMESTAMP_LEN;
- memcpy(buf + offset, msg.text, msg.len);
-
- u32 n = send(serverfd, &buf, buf_len, 0);
- if (n == -1)
- return n;
-
- writef("%d bytes sent.\n", n);
- return n;
-}
-
-u32 receive_message(struct message *msg, u32 clientfd)
-{
- // must all be of the s
- u32 nrecv, buf_len;
- // limit on what can be received with recv()
- u32 buf_size = 20;
- // temporary buffer to receive message data over a stream
- char recv_buf[BUF_MAX];
-
- nrecv = recv(clientfd, recv_buf, buf_size, 0);
- if (nrecv == 0 || nrecv == -1)
- return nrecv;
-
- memcpy(&buf_len, recv_buf, sizeof(buf_len));
-
- u32 i = 0;
- while (nrecv < buf_len) {
- // advance the copying by the amounts of bytes received each time
- i = recv(clientfd, recv_buf + nrecv, buf_size, 0);
- if (i == 0 || i == -1)
- return nrecv;
- nrecv += i;
- }
-
- struct message received = {0};
- memcpy(&received, recv_buf + sizeof(buf_len), MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN);
- received.text = recv_buf + sizeof(buf_len) + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN;
- received.len = buf_len - sizeof(buf_len) - MESSAGE_AUTHOR_LEN - MESSAGE_TIMESTAMP_LEN;
-
- // assume clientfd is serverfd + 1;
- writef("Received %d bytes from client(%d): %s [%s] %s\n", nrecv, clientfd - 3, received.timestamp, received.author, received.text);
- return nrecv;
-}
diff --git a/common.h b/common.h
index 4786c14..e220735 100644
--- a/common.h
+++ b/common.h
@@ -1,5 +1,9 @@
+#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
#define PORT 9983
// max buffer size sent over network
@@ -23,7 +27,7 @@ typedef uint32_t u32;
struct message {
char author[MESSAGE_AUTHOR_LEN];
char timestamp[MESSAGE_TIMESTAMP_LEN]; // HH:MM:SS
- u16 len;
+ u16 len; // length of the text including null terminator
char *text;
};
@@ -47,3 +51,129 @@ u32 send_message(struct message msg, u32 serverfd);
// if recv() returns 0 or -1 it will return early and return 0 or -1 accordingly.
// Otherwise returns the number of bytes received
u32 receive_message(struct message *msg, u32 clientfd);
+
+void writef(char *format, ...)
+{
+ char buf[255 + 1];
+ va_list args;
+ va_start(args, format);
+
+ vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+
+ int n = 0;
+ while (*(buf + n) != 0)
+ n++;
+ write(0, buf, n);
+}
+
+// Returns the length of the string plus the null terminator
+u16 str_len(char *str)
+{
+ if (*str == 0)
+ return 0;
+
+ u16 i = 0;
+ while (str[i])
+ i++;
+
+ return i + 1;
+}
+
+void str_cpy(char *to, char *from)
+{
+ while ((*to++ = *from++))
+ ;
+}
+
+// Save msg to file f
+// Returns 0 on success, returns 1 if msg->text is NULL, returns 2 if mfg->len is 0
+u8 save_message(struct message *msg, FILE *f)
+{
+ if (msg->text == NULL) {
+ return 1;
+ } else if (msg->len == 0)
+ return 2;
+
+ fwrite(&msg->timestamp, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN, 1, f);
+ fwrite(&msg->author, sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
+ fwrite(&msg->len, sizeof(msg->len), 1, f);
+ fputs(msg->text, f);
+
+ return 0;
+}
+
+u8 load_message(struct message *msg, FILE *f)
+{
+ fread(msg, sizeof(*msg->timestamp) * MESSAGE_TIMESTAMP_LEN + sizeof(*msg->author) * MESSAGE_AUTHOR_LEN, 1, f);
+ u16 len;
+ fread(&len, sizeof(len), 1, f);
+ if (len == 0) {
+ // TODO: Error: empty message should not be allowed
+ // empty message
+ msg->text = "";
+ return 1;
+ }
+ char txt[len];
+ fgets(txt, len, f);
+ msg->text = txt;
+
+ return 0;
+}
+
+u32 send_message(struct message msg, u32 serverfd)
+{
+ // stream length : message author : message timestamp : message text + \0
+ u32 buf_len = sizeof(buf_len) + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN + msg.len;
+ char buf[buf_len];
+ u32 offset;
+
+ memcpy(buf, &buf_len, sizeof(buf_len));
+ offset = sizeof(buf_len);
+ memcpy(buf + offset, msg.author, MESSAGE_AUTHOR_LEN);
+ offset += MESSAGE_AUTHOR_LEN;
+ memcpy(buf + offset, msg.timestamp, MESSAGE_TIMESTAMP_LEN);
+ offset += MESSAGE_TIMESTAMP_LEN;
+ memcpy(buf + offset, msg.text, msg.len);
+
+ u32 n = send(serverfd, &buf, buf_len, 0);
+ if (n == -1)
+ return n;
+
+ writef("%d bytes sent.\n", n);
+ return n;
+}
+
+u32 receive_message(struct message *msg, u32 clientfd)
+{
+ // must all be of the s
+ u32 nrecv, buf_len;
+ // limit on what can be received with recv()
+ u32 buf_size = 20;
+ // temporary buffer to receive message data over a stream
+ char recv_buf[BUF_MAX];
+
+ nrecv = recv(clientfd, recv_buf, buf_size, 0);
+ if (nrecv == 0 || nrecv == -1)
+ return nrecv;
+
+ memcpy(&buf_len, recv_buf, sizeof(buf_len));
+
+ u32 i = 0;
+ while (nrecv < buf_len) {
+ // advance the copying by the amounts of bytes received each time
+ i = recv(clientfd, recv_buf + nrecv, buf_size, 0);
+ if (i == 0 || i == -1)
+ return nrecv;
+ nrecv += i;
+ }
+
+ struct message received = {0};
+ memcpy(&received, recv_buf + sizeof(buf_len), MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN);
+ received.text = recv_buf + sizeof(buf_len) + MESSAGE_AUTHOR_LEN + MESSAGE_TIMESTAMP_LEN;
+ received.len = buf_len - sizeof(buf_len) - MESSAGE_AUTHOR_LEN - MESSAGE_TIMESTAMP_LEN;
+
+ // assume clientfd is serverfd + 1;
+ writef("Received %d bytes from client(%d): %s [%s] (%d)%s\n", nrecv, clientfd - 3, received.timestamp, received.author, received.len, received.text);
+ return nrecv;
+}
diff --git a/send.c b/send.c
index 693321e..cea4c2d 100644
--- a/send.c
+++ b/send.c
@@ -26,8 +26,6 @@ void timestamp(char timestamp[MESSAGE_TIMESTAMP_LEN])
strftime(timestamp, MESSAGE_TIMESTAMP_LEN, "%H:%M:%S", ltime);
}
-
-
int main(void)
{
serverfd = socket(AF_INET, SOCK_STREAM, 0);
@@ -45,15 +43,13 @@ int main(void)
struct message input = {
.author = "Friendship",
- .timestamp = ""
};
- input.text = "HII!!";
+ input.text = "Hello from send";
input.len = str_len(input.text);
+ printf("input.len: %d\n", input.len);
timestamp(input.timestamp);
send_message(input, serverfd);
- // send_message(input);
- // send_message(input);
return 0;
}
diff --git a/server.c b/server.c
index 787c6dd..1a24bcc 100644
--- a/server.c
+++ b/server.c
@@ -30,7 +30,7 @@ int main(void)
{
u32 clientfd;
u16 nclient = 0;
- u8 on = 1;
+ u32 on = 1;
struct message msg_recv = {0};
serverfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);