aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2023-12-07 10:50:19 +0200
committerSyndamia <kamen@syndamia.com>2023-12-07 10:50:19 +0200
commit922694d1c64aec4419e9f472469eb32fb5e4f84e (patch)
treebe9f8d2856cdcb9d5a32c9e1edd71fcb375330d8
parentd72624c9af98a6d0700aafb5988759d6736d65d0 (diff)
downloadpico-web-922694d1c64aec4419e9f472469eb32fb5e4f84e.tar
pico-web-922694d1c64aec4419e9f472469eb32fb5e4f84e.tar.gz
pico-web-922694d1c64aec4419e9f472469eb32fb5e4f84e.zip
[all] Initial code concept
-rw-r--r--Makefile10
-rw-r--r--browser.c41
-rw-r--r--server.c56
-rw-r--r--util.c7
-rw-r--r--util.h8
5 files changed, 122 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8853e97
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,10 @@
+.PHONY: all
+all: server browser
+
+.PHONY: server
+server:
+ gcc -o server -I. util.c server.c
+
+.PHONY: browser
+browser:
+ gcc -o browser -I. util.c browser.c
diff --git a/browser.c b/browser.c
new file mode 100644
index 0000000..4fbb9ad
--- /dev/null
+++ b/browser.c
@@ -0,0 +1,41 @@
+/* Receives a markdown file and "renders" it
+ */
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <unistd.h>
+#include <stdio.h>
+
+#include <util.h>
+
+int main(int argc, char* argv[]) {
+ int fd_socket = socket(AF_INET, SOCK_STREAM, 0);
+ if (fd_socket < 0) {
+ perror("socket");
+ return 1;
+ }
+
+ struct sockaddr_in sa_server = {
+ .sin_family = AF_INET,
+ .sin_port = inet_atop("8080"),
+ };
+ if (inet_aton("127.0.0.1", &sa_server.sin_addr.s_addr) < 0) {
+ perror("inet_aton");
+ return 2;
+ }
+
+ if (connect(fd_socket, (struct sockaddr*)&sa_server, sizeof(struct sockaddr_in)) < 0) {
+ perror("connect");
+ return 3;
+ }
+
+ char msg[] = "Hello from browser";
+ write(fd_socket, msg, sizeof(msg));
+
+ char buff[256];
+ read(fd_socket, buff, 256);
+ printf("Received: %s\n", buff);
+
+ close(fd_socket);
+}
diff --git a/server.c b/server.c
new file mode 100644
index 0000000..1393302
--- /dev/null
+++ b/server.c
@@ -0,0 +1,56 @@
+/* The server recieves connections and passes files to the clients
+ */
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <unistd.h>
+#include <stdio.h>
+
+#include <util.h>
+
+int main(int argc, char* argv[]) {
+ int fd_socket = socket(AF_INET, SOCK_STREAM, 0);
+ if (fd_socket < 0) {
+ perror("socket");
+ return 1;
+ }
+
+ struct sockaddr_in sa_socket = {
+ .sin_family = AF_INET,
+ .sin_port = inet_atop("8080"),
+ };
+ if (inet_aton("127.0.0.1", &sa_socket.sin_addr.s_addr) < 0) {
+ perror("inet_aton");
+ return 2;
+ }
+
+ int try_bind = bind(fd_socket, (struct sockaddr*)&sa_socket, sizeof(struct sockaddr_in));
+ if (try_bind < 0) {
+ perror("bind");
+ return 3;
+ }
+
+ int try_listen = listen(fd_socket, 50);
+ if (try_listen < 0) {
+ perror("bind");
+ return 4;
+ }
+
+ struct sockaddr_in sa_client;
+ socklen_t sa_client_size = sizeof(struct sockaddr_in);
+ int fd_client = accept(fd_socket, (struct sockaddr*)&sa_client, &sa_client_size);
+ if (fd_client < 0) {
+ perror("accept");
+ return 5;
+ }
+
+ char buff[256];
+ read(fd_client, buff, 256);
+ printf("Received: %s\n", buff);
+
+ char response[] = "Server received message!";
+ write(fd_client, response, sizeof(response));
+
+ close(fd_socket);
+}
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..c8c8c70
--- /dev/null
+++ b/util.c
@@ -0,0 +1,7 @@
+#include <util.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+
+uint16_t inet_atop(const char *port) {
+ return htons(atoi(port));
+}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..98799ad
--- /dev/null
+++ b/util.h
@@ -0,0 +1,8 @@
+#ifndef H_UTIL
+#define H_UTIL
+
+#include <inttypes.h>
+
+uint16_t inet_atop(const char *port);
+
+#endif