wip with both my and luke code

This commit is contained in:
hugogogo
2022-07-13 17:31:18 +02:00
parent 461c10b6a2
commit 6b1fd5a15b
5 changed files with 99 additions and 78 deletions

View File

@@ -6,7 +6,7 @@
#include <iostream>
#include <exception>
#include <stdexcept>
#include <Webserv.hpp>
#include "Webserv.hpp"
int main(void)
{
@@ -28,47 +28,56 @@ int main(void)
/*
wip hugo version
# include <map>
# include <exception>
# include <stdexcept>
# include <unistd.h> // close
# include <fcntl.h> // fcntl
# include <iostream>
# 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_SCKT -1
# define INVALID_BIND -1
# define INVALID_LSTN -1
# define INVALID_AXPT -1
# define INVALID -1
# define PORT 80
# define NB_CONX 20
# define BACKLOG 20
int main()
{
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int socket_fd;
int new_fd;
int sckt_fd;
int axpt_fd;
int lstn;
socklen_t addr_len;
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == INVALID_SCKT)
// 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(socket_fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
if (socket_fd == INVALID_BIND)
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:
@@ -81,20 +90,31 @@ int main()
// exit(1);
// }
lstn = listen(socket_fd, NB_CONX);
if (lstn == INVALID_LSTN)
// LISTEN ON IT
lstn = listen(sckt_fd, BACKLOG);
if (lstn == INVALID)
{
perror("err listen(): ");
return 0;
}
std::cout << "server listening\n";
new_fd = accept(socket_fd, &their_addr, sizeof(their_addr));
if (new_fd == INVALID_AXPT)
// ACCEPT INCOMING CONNECT
while (1)
{
perror("err accept(): ");
return 0;
}
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";
break;
}
return 0;
}