-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (61 loc) · 2.08 KB
/
Copy pathmain.cpp
File metadata and controls
66 lines (61 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ssghioua <ssghioua@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/01 23:26:13 by ssghioua #+# #+# */
/* Updated: 2026/03/01 23:26:14 by ssghioua ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server.hpp"
#include <exception>
#include <iostream>
#include <sstream>
#include <cerrno>
#include <cstring>
int main(int argc, char **argv) {
if (argc != 2 && argc != 3) {
std::cerr << "Usage: " << argv[0] << " <port> <password>(optional)\n";
return (1);
}
/*!
* @brief Set up signal handlers
*/
struct sigaction act;
act.sa_handler = &Server::sighandler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
/*!
* @brief Set up Ctrl+C signal handlers
*/
if (sigaction(SIGINT, &act, NULL) == -1) {
std::cerr << "Failed to set signal handler: " << std::strerror(errno) << "\n";
return (1);
}
/*!
* @brief Set up Ctrl+\ signal handlers
*/
if (sigaction(SIGQUIT, &act, NULL) == -1) {
std::cerr << "Failed to set signal handler: " << std::strerror(errno) << "\n";
return (1);
}
try {
std::string password = (argc == 3) ? argv[2] : "";
std::istringstream iss(argv[1]);
uint16_t port;
char c;
if (!(iss >> port) || iss >> c) {
std::cerr << "Invalid port number: " << argv[1] << "\n";
return (1);
}
Server myServer(port, password);
myServer.init();
myServer.run();
} catch (std::exception& e) {
std::cerr << e.what() << "\n";
}
std::cout << "Server Closed! Bye Bye!\n";
return (0);
}