From f7f6d572158025684d13fdad9ede6618af9d4cab Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sat, 27 Jan 2024 12:41:02 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 ++ Makefile | 6 ++++++ README.md | 3 +++ client.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++ server.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 client.c create mode 100644 server.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2ad853 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +client +server diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..438b21f --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ + +client: client.c + gcc -o client client.c + +server: server.c + gcc -o server server.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec4efea --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Mini Chat Server + +Tutorial: https://www.youtube.com/watch?v=gGfTjKwLQxY diff --git a/client.c b/client.c new file mode 100644 index 0000000..d4e42df --- /dev/null +++ b/client.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + +#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0])) + +int main() { + int port = 12345; + int stdin_fd = 0; + + int client_fd = socket(AF_INET, SOCK_STREAM, 0); + if (client_fd == -1) { + fprintf(stderr, "Failed to open socket\n"); + return -1; + } + + struct sockaddr_in addr = { AF_INET, htons(port), 0 }; + if (connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + fprintf(stderr, "Failed to connect\n"); + return -1; + } + + while (true) { + struct pollfd fds[] = { + { .fd = stdin_fd , POLLIN, 0 }, + { .fd = client_fd, POLLIN, 0 } + }; + + poll(fds, ARRAY_LEN(fds), -1); + + char buffer[256] = { 0 }; + if (fds[0].revents & POLLIN) { + int read_bytes = read(stdin_fd, buffer, sizeof(buffer) - 1); + send(client_fd, buffer, read_bytes, 0); + } else if (fds[1].revents & POLLIN) { + if (recv(client_fd, buffer, sizeof(buffer) - 1, 0) == 0) { + return 0; + } + + printf("%s\n", buffer); + } + } + + return 0; +} diff --git a/server.c b/server.c new file mode 100644 index 0000000..181e8ff --- /dev/null +++ b/server.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include + +#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0])) + +int main() { + int port = 12345; + int stdin_fd = 0; + + int server_fd = socket(AF_INET, SOCK_STREAM, 0); + if (server_fd == -1) { + fprintf(stderr, "Failed to open socket\n"); + return -1; + } + + int on = 1; + setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + + struct sockaddr_in addr = { AF_INET, htons(port), 0 }; + if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + fprintf(stderr, "Failed to bind\n"); + return -1; + } + + listen(server_fd, 10); + + int client_fd = accept(server_fd, NULL, NULL); + + while (true) { + struct pollfd fds[] = { + { .fd = stdin_fd , POLLIN, 0 }, + { .fd = client_fd, POLLIN, 0 } + }; + + poll(fds, ARRAY_LEN(fds), -1); + + char buffer[256] = { 0 }; + if (fds[0].revents & POLLIN) { + int read_bytes = read(stdin_fd, buffer, sizeof(buffer) - 1); + send(client_fd, buffer, read_bytes, 0); + } else if (fds[1].revents & POLLIN) { + if (recv(client_fd, buffer, sizeof(buffer) - 1, 0) == 0) { + return 0; + } + + printf("%s\n", buffer); + } + } + + return 0; +}