pbm with bind and sudo understood, next is port

This commit is contained in:
hugogogo
2022-07-15 22:14:29 +02:00
parent 6b1fd5a15b
commit ca81b6760e
4 changed files with 42 additions and 49 deletions

View File

@@ -8,8 +8,7 @@ Webserv::Webserv()
if (_socket_fd == -1) if (_socket_fd == -1)
{ {
::perror("err socket(): "); ::perror("err socket(): ");
// throw std::runtime_error("Socket init"); throw std::runtime_error("Socket init");
throw WebservError();
} }
} }
@@ -18,10 +17,6 @@ Webserv::~Webserv()
std::cout << "Server destroyed\n"; std::cout << "Server destroyed\n";
} }
const char * Webserv::WebservError::what() const throw() {
return ("error");
}
/////////////// ///////////////
// Functions // // Functions //
@@ -39,7 +34,8 @@ void Webserv::bind(in_port_t port)
if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1) if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
{ {
::perror("err bind(): "); ::perror("err bind(): ");
// throw std::runtime_error("Socket bind"); throw std::runtime_error("Socket bind");
// std::cerr << "error bind\n";
} }
} }
@@ -48,7 +44,7 @@ void Webserv::listen(unsigned int max_connections)
if (::listen(_socket_fd, max_connections) == -1) if (::listen(_socket_fd, max_connections) == -1)
{ {
::perror("err listen(): "); ::perror("err listen(): ");
// throw std::runtime_error("Socket listen"); throw std::runtime_error("Socket listen");
} }
} }
@@ -63,19 +59,19 @@ void Webserv::start()
int accepted_fd; int accepted_fd;
// poll (or equivalent) // poll (or equivalent)
// struct pollfd poll_s; struct pollfd poll_s;
// poll_s.fd = _socket_fd; poll_s.fd = _socket_fd;
// poll_s.events = POLLIN; poll_s.events = POLLIN;
// char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string. char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string.
// int ret; int ret;
std::cout << "Server started\n"; std::cout << "Server started\n";
while (1) while (1)
{ {
// std::cout << "----------\n"; std::cout << "----------\n";
// std::cout << "poll()\n"; // poll (or equivalent) std::cout << "poll()\n"; // poll (or equivalent)
// ::poll(&poll_s, 1, -1); ::poll(&poll_s, 1, -1);
addr_len = sizeof addr; addr_len = sizeof addr;
accepted_fd = ::accept(_socket_fd, (sockaddr*)&addr, &addr_len); accepted_fd = ::accept(_socket_fd, (sockaddr*)&addr, &addr_len);
@@ -87,32 +83,32 @@ void Webserv::start()
} }
std::cout << "accept()\n"; std::cout << "accept()\n";
// // "Your server must never block and the client can be bounced properly if necessary". // "Your server must never block and the client can be bounced properly if necessary".
// // NO-Block OK, but how to handle it ? Miss the bouncing part. // NO-Block OK, but how to handle it ? Miss the bouncing part.
// ::fcntl(accepted_fd, F_SETFL, O_NONBLOCK); ::fcntl(accepted_fd, F_SETFL, O_NONBLOCK);
//
// std::cout << "recv()\n"; std::cout << "recv()\n";
// ret = ::recv(accepted_fd, buf, BUFSIZE, 0); ret = ::recv(accepted_fd, buf, BUFSIZE, 0);
// if (ret == -1) if (ret == -1)
// { {
// ::perror("err recv(): "); ::perror("err recv(): ");
// if (::send(accepted_fd, MSG_BOUNCE, sizeof MSG_BOUNCE - 1, 0) == -1) if (::send(accepted_fd, MSG_BOUNCE, sizeof MSG_BOUNCE - 1, 0) == -1)
// ::perror("err send(): "); ::perror("err send(): ");
// ::close(accepted_fd); ::close(accepted_fd);
// continue; continue;
// } }
// /* /*
// if (ret == BUFSIZE) if (ret == BUFSIZE)
// // send error like "request too long" to client // send error like "request too long" to client
// */ */
// buf[ret] = '\0'; buf[ret] = '\0';
//
// std::cout << "send()\n"; std::cout << "send()\n";
// if (::send(accepted_fd, buf, ret, 0) == -1) // echo the read if (::send(accepted_fd, buf, ret, 0) == -1) // echo the read
// ::perror("err send(): "); ::perror("err send(): ");
// if (::send(accepted_fd, MSG_TEST, sizeof MSG_TEST - 1, 0) == -1) if (::send(accepted_fd, MSG_TEST, sizeof MSG_TEST - 1, 0) == -1)
// ::perror("err send(): "); ::perror("err send(): ");
//
// ::close(accepted_fd); ::close(accepted_fd);
} }
} }

View File

@@ -36,10 +36,6 @@ class Webserv
std::map<std::string, std::string> _request; std::map<std::string, std::string> _request;
std::map<std::string, std::string> _response; std::map<std::string, std::string> _response;
class WebservError : public std::exception {
const char * what() const throw();
};
}; };
#endif #endif

View File

@@ -14,7 +14,8 @@ int main(void)
{ {
Webserv serv; Webserv serv;
serv.bind(80); // https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
serv.bind(8080);
serv.listen(512); // 512 max connections arbitrary serv.listen(512); // 512 max connections arbitrary
serv.start(); serv.start();
} }
@@ -44,7 +45,7 @@ int main(void)
# include <poll.h> // poll # include <poll.h> // poll
# define INVALID -1 # define INVALID -1
# define PORT 80 # define PORT 1025
# define BACKLOG 20 # define BACKLOG 20
int main() int main()

BIN
webserv

Binary file not shown.