summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2022-04-07 14:36:21 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2022-04-07 14:36:21 +0200
commit33e14b74df70ecbfcdb56b71a0ea7d4f1e05b1f4 (patch)
tree46b8d456a6eda31b90ec1b2282bc696e03bd7f86
parentb3b3d2f7a9365a1c9c4ccdf9ffa8f0bcf83f1080 (diff)
server.c: Create server struct
-rw-r--r--.gitignore2
-rw-r--r--Makefile2
-rw-r--r--main.c38
-rw-r--r--server.c41
-rw-r--r--server.h12
5 files changed, 64 insertions, 31 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..894d0db
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+papo
+*.o
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b2c1d88
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+all: main.c server.h
+ $(CC) -g -o papo main.c server.c
diff --git a/main.c b/main.c
index a977641..5db8d59 100644
--- a/main.c
+++ b/main.c
@@ -24,46 +24,22 @@
#include <sys/socket.h>
#include <unistd.h>
+#include "./server.h"
+
#define SERVER_PORT 6667
#define BUFFER_SIZE 1024
#define EXIT_COMMAND "exit"
int main() {
- int server_fd = socket(AF_INET, SOCK_STREAM, 0);
- if (server_fd == -1) {
- perror("[ERROR] unable to create a socket");
- return EXIT_FAILURE;
- }
-
- struct sockaddr_in server;
- server.sin_addr.s_addr = htonl(INADDR_ANY);
- server.sin_family = AF_INET;
- server.sin_port = htons(SERVER_PORT);
-
- int opt_val = 1;
- setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val));
-
- if (bind(server_fd, (struct sockaddr *) &server, sizeof(server)) == -1) {
- perror("[ERROR] could not bind");
- close(server_fd);
- return EXIT_FAILURE;
- }
-
- if (listen(server_fd, SOMAXCONN) == -1) {
- perror("[ERROR] could not listen");
- close(server_fd);
- return EXIT_FAILURE;
- }
-
- printf("[INFO] server listening at port (%d)\n", SERVER_PORT);
+ server_t server = server_create(SERVER_PORT);
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
- int client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
+ int client_fd = accept(server.fd, (struct sockaddr *) &client, &client_len);
if (client_fd == -1) {
perror("[ERROR] could not accept connection");
- close(server_fd);
+ close(server.fd);
return EXIT_FAILURE;
}
@@ -81,7 +57,7 @@ int main() {
if (!strncasecmp(client_buf, EXIT_COMMAND, strlen(EXIT_COMMAND) - 1)) {
puts("[INFO] exiting program. bye bye!");
close(client_fd);
- close(server_fd);
+ close(server.fd);
exit(EXIT_SUCCESS);
}
@@ -92,6 +68,6 @@ int main() {
}
close(client_fd);
- close(server_fd);
+ close(server.fd);
return EXIT_SUCCESS;
}
diff --git a/server.c b/server.c
new file mode 100644
index 0000000..d7edb04
--- /dev/null
+++ b/server.c
@@ -0,0 +1,41 @@
+#include "server.h"
+
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+server_t
+server_create(uint32_t port)
+{
+ int server_fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (server_fd == -1) {
+ perror("[ERROR] unable to create a socket");
+ exit(EXIT_FAILURE);
+ }
+
+ struct sockaddr_in server;
+ server.sin_addr.s_addr = htonl(INADDR_ANY);
+ server.sin_family = AF_INET;
+ server.sin_port = htons(port);
+
+ int opt_val = 1;
+ setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val));
+
+ if (bind(server_fd, (struct sockaddr *) &server, sizeof(server)) == -1) {
+ perror("[ERROR] could not bind");
+ close(server_fd);
+ exit(EXIT_FAILURE);
+ }
+
+ if (listen(server_fd, SOMAXCONN) == -1) {
+ perror("[ERROR] could not listen");
+ close(server_fd);
+ exit(EXIT_FAILURE);
+ }
+
+ printf("[INFO] server listening at port (%d)\n", port);
+
+ return (server_t) { .fd = server_fd };
+}
diff --git a/server.h b/server.h
new file mode 100644
index 0000000..1b788dd
--- /dev/null
+++ b/server.h
@@ -0,0 +1,12 @@
+#ifndef SERVER_H
+#define SERVER_H
+
+#include <stdint.h>
+
+typedef struct server {
+ int fd;
+} server_t;
+
+server_t server_create(uint32_t port);
+
+#endif /* SERVER_H */