separate main tests in differents files and added comparison in readme

This commit is contained in:
hugogogo
2022-07-20 14:34:16 +02:00
parent 726bf36b17
commit fecf5411bc
10 changed files with 1145 additions and 130 deletions

View File

@@ -1,7 +1,4 @@
/*
luke version
#include <iostream>
#include <exception>
#include <stdexcept>
@@ -13,8 +10,8 @@ int main(void)
{
Webserv serv;
// https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
serv.bind(8080);
// https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
serv.bind(4040);
serv.listen(512); // 512 max connections arbitrary
serv.start();
}
@@ -24,112 +21,46 @@ int main(void)
}
return (0);
}
*/
/*
wip hugo version
______
listen_fd = SOCKET() : create a listening socket
__________
SETSOCKOPT() : Allow socket descriptor to be reuseable
_____
IOCTL() : set listen_fd and all incoming socket to be non-blocking
____
BIND(port) : associate listen_fd to a port
______
LISTEN(nb_queue) : queue the incoming connections to listen_fd, up to a chosen number
fds[1] = listen_fd
loop
. ____
. POLL(fds[]) :
.
. loop through fds[]
. .
. . POLLIN && listen_fd ? : readable socket and this is the listening one
. . . ______
. . . new_fd = ACCEPT() : extract first connection request in queue of listen_fd
. . . and creates a new socket that is connected
. . .
. . . fds[] += new_fd
. .
. . POLLIN ? : readable socket and this is an active one
. . . ____
. . . RECV() : read data in socket created by accept()
. . . ____
. . . SEND() : write data in socket created by accept()
loop through fds[] :
. _____
. CLOSE(fds[])
*/
# include <map>
# include <exception>
# include <stdexcept>
# include <fcntl.h> // fcntl
# include <unistd.h> // close
# include <stdlib.h> // exit
# include <iostream> // cout, cin
# include <cerrno> // errno
# include <cstdio> // perror
# include <string.h> // memset
# include <sys/socket.h> // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname
# include <netinet/in.h> // sockaddr_in
# include <arpa/inet.h> // inet_ntoa, inet_addr, htonl, htons, ntohl, ntohs
# include <poll.h> // poll
# define INVALID -1
# define BACKLOG 20
// test with curl http://localhost:PORT (replace PORT by the port number you choose below)
# define PORT 4040
int main()
{
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sckt_fd;
int axpt_fd;
int lstn;
socklen_t addr_len;
// INIT SOCKET
sckt_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (sckt_fd == INVALID)
{
perror("err socket(): ");
return 0;
}
std::cout << "server init\n";
// BIND IT
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// my_addr.sin_addr.s_addr = inet_addr("10.12.110.57");
bind(sckt_fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
if (sckt_fd == INVALID)
{
perror("err bind(): ");
return 0;
}
std::cout << "socket bind to port: " << PORT << "\n";
// https://beej.us/guide/bgnet/html/index-wide.html#cb29 :
//
// Sometimes, you might notice, you try to rerun a server and bind() fails, claiming “Address already in use.” What does that mean? Well, a little bit of a socket that was connected is still hanging around in the kernel, and its hogging the port. You can either wait for it to clear (a minute or so), or add code to your program allowing it to reuse the port, like this:
//
// int yes=1;
//
// // lose the pesky "Address already in use" error message
// if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof yes) == -1) {
// perror("setsockopt");
// exit(1);
// }
// LISTEN ON IT
lstn = listen(sckt_fd, BACKLOG);
if (lstn == INVALID)
{
perror("err listen(): ");
return 0;
}
std::cout << "server listening\n";
// ACCEPT INCOMING CONNECT
while (1)
{
addr_len = sizeof(their_addr);
axpt_fd = accept(sckt_fd, (sockaddr*)&their_addr, &addr_len);
if (axpt_fd == INVALID)
{
perror("err accept(): ");
continue;
}
std::cout << "server accepted a socket from: "
<< inet_ntoa(their_addr.sin_addr)
<< "\n";
// // with a fork
// if (!fork()) // child process
// {
// close(sckt_fd); // child doesn't need the listener
// if (send(axpt_fd, "hello world !", 13, 0) == INVALID)
// perror("err send(): ");
// close(axpt_fd);
// exit(0);
// }
// close(axpt_fd); // parent doesn't need this
}
return 0;
}