Created a functional basic constructor for Server class
This commit is contained in:
@@ -6,35 +6,61 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/06 18:22:36 by iherman- ### ########.fr */
|
/* Updated: 2026/05/07 13:00:56 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
|
|
||||||
Server::Server()
|
const int Server::kConnectionQueueLimit = 10;
|
||||||
|
|
||||||
|
Server::Server() :
|
||||||
|
port_(PORT_DEFAULT),
|
||||||
|
password_("")
|
||||||
{
|
{
|
||||||
std::cout << "Hello from server" << std::endl;
|
// std::cout << "Hello from server" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::Server(int port, const std::string& password)
|
Server::Server(int port, const std::string& password) :
|
||||||
|
port_(port),
|
||||||
|
password_(password)
|
||||||
{
|
{
|
||||||
if (port < 1 || port > 65535)
|
if (port_ < 1 || port_ > 65535)
|
||||||
throw std::runtime_error("Invalid port");
|
throw std::runtime_error("Invalid port");
|
||||||
|
|
||||||
std::cout << "Server port: " << port << "\nServer Password: " << password << std::endl;
|
if (password_.empty())
|
||||||
|
throw std::runtime_error("Empty password");
|
||||||
|
|
||||||
|
serverSocket_ = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (serverSocket_ < 0)
|
||||||
|
throw std::runtime_error("Failed to create socket");
|
||||||
|
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
std::memset(&addr, 0, sizeof(addr));
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port_);
|
||||||
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr));
|
||||||
|
|
||||||
|
std::cout << "Server port: " << port_
|
||||||
|
<< "\nServer Password: " << password_
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server()
|
Server::~Server()
|
||||||
{
|
{
|
||||||
|
close(serverSocket_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Server &Server::operator=(const Server& other)
|
Server &Server::operator=(const Server& other)
|
||||||
{
|
{
|
||||||
if (this != &other)
|
if (this != &other)
|
||||||
{
|
{
|
||||||
std::cout << "operator= called" << std::endl;
|
port_ = other.port_;
|
||||||
|
serverSocket_ = other.serverSocket_;
|
||||||
|
password_ = other.password_;
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/06 17:41:04 by iherman- ### ########.fr */
|
/* Updated: 2026/05/07 13:00:39 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -33,14 +33,22 @@
|
|||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
|
||||||
// C++ lib functions
|
// C++ lib functions
|
||||||
#include <iostream>
|
# include <iostream>
|
||||||
|
|
||||||
# include <string>
|
# include <string>
|
||||||
# include <sstream>
|
# include <cstring>
|
||||||
|
|
||||||
|
# define PORT_DEFAULT 9898
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
static const int kConnectionQueueLimit;
|
||||||
|
|
||||||
|
int port_;
|
||||||
|
int serverSocket_;
|
||||||
|
|
||||||
|
std::string password_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server();
|
Server();
|
||||||
@@ -48,6 +56,8 @@ class Server
|
|||||||
~Server();
|
~Server();
|
||||||
|
|
||||||
Server &operator=(const Server& other);
|
Server &operator=(const Server& other);
|
||||||
|
|
||||||
|
void run();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SERVER_HPP
|
#endif // SERVER_HPP
|
||||||
3
main.cpp
3
main.cpp
@@ -6,11 +6,12 @@
|
|||||||
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/05/06 17:57:53 by iherman- #+# #+# */
|
/* Created: 2026/05/06 17:57:53 by iherman- #+# #+# */
|
||||||
/* Updated: 2026/05/06 17:57:58 by iherman- ### ########.fr */
|
/* Updated: 2026/05/07 12:44:36 by iherman- ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "Server/Server.hpp"
|
#include "Server/Server.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
int get_port(const char* arg)
|
int get_port(const char* arg)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user