Empezando proyecto

This commit is contained in:
aortigos
2026-04-14 01:01:01 +02:00
commit b39245ddf1
4 changed files with 161 additions and 0 deletions

42
Makefile Normal file
View File

@@ -0,0 +1,42 @@
NAME = ircserver
SRC = main.cpp Server/Server.cpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Eliminando...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

59
Server/Server.cpp Normal file
View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/13 22:31:44 by aortigos #+# #+# */
/* Updated: 2026/04/13 22:31:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server.hpp"
//////////////////
// Constructors //
//////////////////
Server::Server()
{
std::cout << "Server default constructor called" << std::endl;
}
Server::Server(int port) : port(port)
{
std::cout << "Server will use port: " << port << std::endl;
}
Server::~Server()
{
std::cout << "Server destructor called" << std::endl;
}
int Server::run()
{
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(this->port);
addr.sin_addr.s_addr = INADDR_ANY;
bind(server_fd, (sockaddr*)&addr, sizeof(addr));
listen(server_fd, 1);
std::cout << "Esperando conexion" << std::endl;
int client_fd = accept(server_fd, NULL, NULL);
std::cout << "Cliente conectado" << std::endl;
char buf[512] = {0};
recv(client_fd, buf, sizeof(buf), 0);
std::cout << "Mensaje: " << buf << std::endl;
close(client_fd);
close(server_fd);
return (0);
}

33
Server/Server.hpp Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/13 22:31:44 by aortigos #+# #+# */
/* Updated: 2026/04/13 22:31:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SERVER_HPP
# define SERVER_HPP
# include <iostream>
# include <cstdlib>
# include <sys/socket.h>
# include <netinet/in.h>
# include <unistd.h>
class Server
{
private:
int port;
public:
Server();
Server(int port);
~Server();
int run();
};
#endif

27
main.cpp Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/13 22:31:53 by aortigos #+# #+# */
/* Updated: 2026/04/14 00:47:23 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server/Server.hpp"
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout << "Port is needed" << std::endl;
return (1);
}
Server sv(atoi(argv[1]));
sv.run();
return (0);
}