aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-21 00:16:33 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-10-21 00:24:23 +0200
commitb42d245a5c408e487f132a7ee84c9f4627c5b889 (patch)
treea91762f3c841f5c14bfb4470c4671c2722948e46
parentd4e7c6876eed2733a2678668bdcabdd87659e826 (diff)
Added minimal send implementation
Renamed minirecv.c to recv.c Added build commands to build.sh Added the use of common code
-rw-r--r--.gitignore2
-rw-r--r--README.md1
-rwxr-xr-xbuild.sh2
-rw-r--r--recv.c (renamed from minirecv.c)36
-rw-r--r--send.c63
5 files changed, 41 insertions, 63 deletions
diff --git a/.gitignore b/.gitignore
index 79557cf..fd52ccf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
chatty
server
+recv
+send
tags
tmp
diff --git a/README.md b/README.md
index 2b02f02..2eb012e 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,6 @@
# Chatty
- use memory arena's to manage memory
+- spec for the stream protocol
# Server
The idea is the following:
diff --git a/build.sh b/build.sh
index 60a538d..bfd8c19 100755
--- a/build.sh
+++ b/build.sh
@@ -2,3 +2,5 @@
set -x
gcc -g -Wall -pedantic -std=c99 -o chatty client.c common.c
gcc -g -Wall -pedantic -std=c99 -o server server.c common.c
+gcc -g -Wall -pedantic -std=c99 -o send send.c common.c
+gcc -g -Wall -pedantic -std=c99 -o recv recv.c common.c
diff --git a/minirecv.c b/recv.c
index bba373a..e4433ae 100644
--- a/minirecv.c
+++ b/recv.c
@@ -1,18 +1,20 @@
+// Minimal server implementation for probing out things
+
#include "common.h"
#include <arpa/inet.h>
+#include <errno.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
-#include <errno.h>
int main(void)
{
- int serverfd, clientfd;
- int on = 1;
+ u32 serverfd, clientfd;
+ u8 on = 1;
const struct sockaddr_in address = {
AF_INET,
- htons(9999),
+ htons(PORT),
{0},
};
@@ -23,9 +25,7 @@ int main(void)
listen(serverfd, 256);
- writef("serverfd: %d\n", serverfd);
clientfd = accept(serverfd, 0, 0);
- writef("clientfd: %d\n", clientfd);
struct pollfd fds[1] = {
{clientfd, POLLIN, 0},
@@ -37,29 +37,19 @@ int main(void)
return 2;
if (fds[0].revents & POLLIN) {
- int nrecv;
-
- char buf[20];
-
- nrecv = recv(clientfd, buf, sizeof(buf), 0);
- printf("received %d bytes\n", nrecv);
- nrecv = recv(clientfd, buf, sizeof(buf), 0);
- printf("received %d bytes\n", nrecv);
- nrecv = recv(clientfd, buf, sizeof(buf), 0);
- printf("received %d bytes\n", nrecv);
-
- return 3;
+ u8 recv_buf[BUF_MAX];
+ u32 nrecv = recv(clientfd, recv_buf, sizeof(recv_buf), 0);
+ writef("client(%d): %d bytes received.\n", clientfd, nrecv);
if (nrecv == -1) {
return errno;
} else if (nrecv == 0) {
- writef("Disconnect.\n");
- fds[0].fd = -1;
+ writef("client(%d): disconnected.\n", clientfd);
+ fds[0].fd = -1;
fds[0].revents = 0;
close(clientfd);
- }
-
- writef("received: %d bytes\n", nrecv);
+ return 0;
+ }
}
}
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;
}