Zappy - Year end project 2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
server_initialization.c
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** my_zappy
4** File description:
5** server_initialization
6*/
7
8#include <stdio.h>
9#include <string.h>
10#include <arpa/inet.h>
11#include <sys/socket.h>
12
13#include "utils.h"
14#include "server_handler.h"
15
16static int bind_server(server_handler_t *server, char *value)
17{
18 struct sockaddr_in addr;
19
20 addr.sin_family = AF_INET;
21 addr.sin_port = htons(atoi(value));
22 addr.sin_addr.s_addr = INADDR_ANY;
23 if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
24 if (shutdown(server->socket, SHUT_RDWR) == -1) {
25 perror("shutdown");
26 return (-1);
27 }
28 perror("bind");
29 return (-1);
30 }
31 return (0);
32}
33
34static int init_server_queue(server_handler_t *server)
35{
36 if (listen(server->socket, MAX_CLIENT) == -1) {
37 if (shutdown(server->socket, SHUT_RDWR) == -1) {
38 perror("shutdown");
39 return (-1);
40 }
41 perror("listen");
42 return (-1);
43 }
44 return (0);
45}
46
47int server_initialization(server_handler_t *server, struct arg_s **arguments)
48{
49 char *value = NULL;
50
51 server->socket = socket(AF_INET, SOCK_STREAM, 0);
52 if (server->socket == -1) {
53 perror("socket");
54 return (-1);
55 }
56 value = (char *)find_value_by_param((const struct arg_s **)arguments,
57 "-p");
58 if (bind_server(server, value) == -1) {
59 return (-1);
60 }
61 if (init_server_queue(server) == -1) {
62 return (-1);
63 }
64 for (int i = 0; i < MAX_CLIENT; i++) {
65 server->fd_queue[i] = UNKNOWN;
66 }
67 return init_game_data(server, arguments);
68}
#define MAX_CLIENT
Definition constants.h:15
#define UNKNOWN
Definition constants.h:16
int init_game_data(server_handler_t *server, struct arg_s **arguments)
The function to initialize every game data.
int server_initialization(server_handler_t *server, struct arg_s **arguments)
The function to initialize every server data.
A structure to store server general data.
int fd_queue[MAX_CLIENT]
void * find_value_by_param(const struct arg_s **arguments, char *param)
! FINDER !!