wip put poll version inside luke version

This commit is contained in:
hugogogo
2022-07-20 17:11:24 +02:00
parent fecf5411bc
commit 708da8bb61
6 changed files with 523 additions and 334 deletions

View File

@@ -80,13 +80,16 @@
## code architecture ## code architecture
``` ```
functions action in this scenario :
______ ______
sd = SOCKET() : create a listening socket descriptor sd = SOCKET() : create a listening socket descriptor
__________ __________
SETSOCKOPT(sd) : allow socket descriptor to be reuseable SETSOCKOPT(sd) : allow socket descriptor to be reuseable
_____ _____
IOCTL(sd) : set listening socket and all incoming socket to be non-blocking IOCTL(sd) : set listening socket and all incoming socket to be non-blocking
_____
FCNTL(sd) : set listening socket and all incoming socket to be non-blocking
____ ____
BIND(port) : associate listening socket to a port BIND(port) : associate listening socket to a port
______ ______
@@ -115,7 +118,7 @@
compare architectures : compare architectures :
``` ```
POLL SELECT POLL SELECT
______ ______ ______ ______ ______ ______
lstn_sd = SOCKET() | lstn_sd = SOCKET() | lstn_sd = SOCKET() lstn_sd = SOCKET() | lstn_sd = SOCKET() | lstn_sd = SOCKET()
| __________ | __________ | __________ | __________
@@ -138,6 +141,7 @@ compare architectures :
. | . loop through fds[] | . loop i++ < max_fd . | . loop through fds[] | . loop i++ < max_fd
. | . . | . . . | . . | . .
. | . . POLLIN && lstn_sd ? | . . FD_ISSET(i) & lstn_fd ? . | . . POLLIN && lstn_sd ? | . . FD_ISSET(i) & lstn_fd ?
. | . . loop | . . loop
. ______ | . . . ______ | . . . ______ . ______ | . . . ______ | . . . ______
. ACCEPT() | . . . new_sd = ACCEPT() | . . . new_sd = ACCEPT() . ACCEPT() | . . . new_sd = ACCEPT() | . . . new_sd = ACCEPT()
. | . . . | . . . . | . . . | . . .
@@ -145,7 +149,8 @@ compare architectures :
. | . . | . . . . | . . | . . .
. | . . | . . . max_sd = new_sd . | . . | . . . max_sd = new_sd
. | . . | . . . | . . | . .
. | . . POLLIN ? | . . FD_ISSET ? . | . . or POLLIN ? | . . or FD_ISSET ?
. | . . loop | . . loop
. ____ | . . . ____ | . . . ____ . ____ | . . . ____ | . . . ____
. RECV() | . . . RECV() | . . . RECV() . RECV() | . . . RECV() | . . . RECV()
. ____ | . . . ____ | . . . ____ . ____ | . . . ____ | . . . ____
@@ -156,5 +161,10 @@ compare architectures :
. CLOSE(fds[]) | . CLOSE(fds[]) | . CLOSE(fds[]) . CLOSE(fds[]) | . CLOSE(fds[]) | . CLOSE(fds[])
first, create the socket, add some options, bind it, and listen to it.
then, do a loop starting with POLL or SELECT
if it's incomming connexions, accept and add them all to the list
if it's accepted connexions, read their datas and write new datas
``` ```

View File

@@ -3,30 +3,41 @@
Webserv::Webserv() Webserv::Webserv()
{ {
int on = 1;
std::cout << "Server init\n"; std::cout << "Server init\n";
// _socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
_socket_fd = ::socket(AF_INET, SOCK_STREAM, 0); //_socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
_socket_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
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");
} }
// allow socket descriptor to be reuseable
if (setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
{
::perror("setsockopt() failed");
throw std::runtime_error("Socket init");
}
} }
/* Webserv::Webserv(Webserv const &src) //Webserv::Webserv(Webserv const &src)
{ //{
//
} */ //}
Webserv::~Webserv() Webserv::~Webserv()
{ {
std::cout << "Server destroyed\n"; std::cout << "Server destroyed\n";
} }
/* Webserv & Webserv::operator=(Webserv const &rhs) //Webserv & Webserv::operator=(Webserv const &rhs)
{ //{
//
} */ //}
/////////////// ///////////////
@@ -34,13 +45,10 @@ Webserv::~Webserv()
void Webserv::bind(in_port_t port) void Webserv::bind(in_port_t port)
{ {
// cast invalid ? how to ?
// const struct sockaddr* cast_test = static_cast<const struct sockaddr*>(addr);
struct sockaddr_in addr; struct sockaddr_in addr;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = ::htons(port); addr.sin_port = ::htons(port);
addr.sin_addr.s_addr = ::htonl(INADDR_ANY); // htonl useless with 0 value (INADDR_ANY) addr.sin_addr.s_addr = ::htonl(INADDR_ANY);
if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1) if (::bind(_socket_fd, (const sockaddr*)&addr, sizeof addr) == -1)
{ {
@@ -61,19 +69,221 @@ void Webserv::listen(unsigned int max_connections)
#define BUFSIZE 8192 #define BUFSIZE 8192
#define MSG_TEST "Le Webserv / 20 =D\n" #define MSG_TEST "Le Webserv / 20 =D\n"
#define MSG_BOUNCE "bounced properly ;)\n" // placeholder #define MSG_BOUNCE "bounced properly ;)\n" // placeholder
#define TRUE 1
#define FALSE 0
void Webserv::start() void Webserv::start()
{ {
struct sockaddr_in addr; int len, rc;
socklen_t addr_len; int listen_sd = -1, new_sd = -1;
int accepted_fd; int end_server = FALSE, compress_array = FALSE;
struct pollfd poll_s; int close_conn;
char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string. char buffer[80];
int ret; // struct sockaddr_in addr;
struct pollfd fds[200];
int nfds = 1, current_size = 0, i, j;
memset(fds, 0 , sizeof(fds));
fds[0].fd = listen_sd;
fds[0].events = POLLIN;
std::cout << "Server started\n"; std::cout << "Server started\n";
while (1) while (end_server == FALSE)
{ {
// ***********************************************************
// * Call poll() *
// ***********************************************************
poll(fds, nfds, -1);
// ***********************************************************
// * One or more descriptors are readable. Need to *
// * determine which ones they are. *
// ***********************************************************
current_size = nfds;
for (i = 0; i < current_size; i++)
{
// *********************************************************
// * Loop through to find the descriptors that returned *
// * POLLIN and determine whether it's the listening *
// * or the active connection. *
// *********************************************************
if(fds[i].revents == 0)
continue;
// *********************************************************
// * If revents is not POLLIN, it's an unexpected result, *
// * log and end the server. *
// *********************************************************
if(fds[i].revents != POLLIN)
{
printf(" Error! revents = %d\n", fds[i].revents);
end_server = TRUE;
break;
}
if (fds[i].fd == listen_sd)
{
// *******************************************************
// * Listening descriptor is readable. *
// *******************************************************
printf(" Listening socket is readable\n");
// *******************************************************
// * Accept all incoming connections that are *
// * queued up on the listening socket before we *
// * loop back and call poll again. *
// *******************************************************
do
{
// *****************************************************
// * Accept each incoming connection. If *
// * accept fails with EWOULDBLOCK, then we *
// * have accepted all of them. Any other *
// * failure on accept will cause us to end the *
// * server. *
// *****************************************************
new_sd = accept(listen_sd, NULL, NULL);
if (new_sd < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" accept() failed");
end_server = TRUE;
}
break;
}
// *****************************************************
// * Add the new incoming connection to the *
// * pollfd structure *
// *****************************************************
printf(" New incoming connection - %d\n", new_sd);
fds[nfds].fd = new_sd;
fds[nfds].events = POLLIN;
nfds++;
// *****************************************************
// * Loop back up and accept another incoming *
// * connection *
// *****************************************************
} while (new_sd != -1);
}
// *********************************************************
// * This is not the listening socket, therefore an *
// * existing connection must be readable *
// *********************************************************
else
{
printf(" Descriptor %d is readable\n", fds[i].fd);
close_conn = FALSE;
// *******************************************************
// * Receive all incoming data on this socket *
// * before we loop back and call poll again. *
// *******************************************************
do
{
// *****************************************************
// * Receive data on this connection until the *
// * recv fails with EWOULDBLOCK. If any other *
// * failure occurs, we will close the *
// * connection. *
// *****************************************************
rc = recv(fds[i].fd, buffer, sizeof(buffer), 0);
if (rc < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" recv() failed");
close_conn = TRUE;
}
break;
}
// *****************************************************
// * Check to see if the connection has been *
// * closed by the client *
// *****************************************************
if (rc == 0)
{
printf(" Connection closed\n");
close_conn = TRUE;
break;
}
// *****************************************************
// * Data was received *
// *****************************************************
len = rc;
printf(" %d bytes received\n", len);
// *****************************************************
// * Echo the data back to the client *
// *****************************************************
rc = send(fds[i].fd, buffer, len, 0);
if (rc < 0)
{
perror(" send() failed");
close_conn = TRUE;
break;
}
} while(TRUE);
// *******************************************************
// * If the close_conn flag was turned on, we need *
// * to clean up this active connection. This *
// * clean up process includes removing the *
// * descriptor. *
// *******************************************************
if (close_conn)
{
close(fds[i].fd);
fds[i].fd = -1;
compress_array = TRUE;
}
} // End of existing connection is readable
} // End of loop through pollable descriptors
// ***********************************************************
// * If the compress_array flag was turned on, we need *
// * to squeeze together the array and decrement the number *
// * of file descriptors. We do not need to move back the *
// * events and revents fields because the events will always*
// * be POLLIN in this case, and revents is output. *
// ***********************************************************
if (compress_array)
{
compress_array = FALSE;
for (i = 0; i < nfds; i++)
{
if (fds[i].fd == -1)
{
for(j = i; j < nfds; j++)
{
fds[j].fd = fds[j+1].fd;
}
i--;
nfds--;
}
}
}
}
/*
struct sockaddr_in addr;
socklen_t addr_len;
int accepted_fd;
struct pollfd poll_s;
char buf[BUFSIZE]; // WIP buffer. need to try with std::vector or std::string.
int ret;
std::cout << "----------\n"; std::cout << "----------\n";
std::cout << "accept()\n"; std::cout << "accept()\n";
addr_len = sizeof addr; addr_len = sizeof addr;
@@ -102,10 +312,6 @@ void Webserv::start()
::close(accepted_fd); ::close(accepted_fd);
continue; continue;
} }
/*
if (ret == BUFSIZE)
// send error like "request too long" to client
*/
buf[ret] = '\0'; buf[ret] = '\0';
std::cout << "send()\n"; std::cout << "send()\n";
@@ -115,5 +321,6 @@ void Webserv::start()
::perror("err send(): "); ::perror("err send(): ");
::close(accepted_fd); ::close(accepted_fd);
*/
} }
} }

View File

@@ -4,8 +4,6 @@
# include <string> # include <string>
# include <map> # include <map>
# include <cerrno> // errno
# include <cstdio> // perror
# include <exception> # include <exception>
# include <stdexcept> # include <stdexcept>
# include <unistd.h> // close # include <unistd.h> // close
@@ -19,6 +17,19 @@
# include <poll.h> // poll # include <poll.h> // poll
# include <fcntl.h> // fcntl # 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
# include <fcntl.h> // fcntl
# include <sys/ioctl.h> // ioctl
class Webserv class Webserv
{ {
public: public:

View File

@@ -12,7 +12,7 @@ int main(void)
// https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost // https://security.stackexchange.com/questions/169213/how-to-chose-a-port-to-run-an-application-on-localhost
serv.bind(4040); serv.bind(4040);
serv.listen(512); // 512 max connections arbitrary serv.listen(20);
serv.start(); serv.start();
} }
catch (std::exception& e) catch (std::exception& e)
@@ -22,45 +22,3 @@ int main(void)
return (0); return (0);
} }
/*
______
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[])
*/

View File

@@ -1,4 +1,6 @@
// https://www.ibm.com/docs/en/i/7.2?topic=designs-example-nonblocking-io-select
# include <unistd.h> // close # include <unistd.h> // close
# include <string.h> # include <string.h>
# include <stdio.h> # include <stdio.h>
@@ -16,288 +18,289 @@
int main () int main ()
{ {
int i, len, rc, on = 1; int i, len, rc;
int listen_sd, max_sd, new_sd; int on = 1;
int desc_ready, end_server = FALSE; int listen_sd, max_sd, new_sd;
int close_conn; int desc_ready, end_server = FALSE;
char buffer[80]; int close_conn;
struct sockaddr_in6 addr; char buffer[80];
struct timeval timeout; struct sockaddr_in6 addr;
fd_set master_set, working_set; struct timeval timeout;
fd_set master_set, working_set;
/*************************************************************/ /*************************************************************/
/* Create an AF_INET6 stream socket to receive incoming */ /* Create an AF_INET6 stream socket to receive incoming */
/* connections on */ /* connections on */
/*************************************************************/ /*************************************************************/
listen_sd = socket(AF_INET6, SOCK_STREAM, 0); listen_sd = socket(AF_INET6, SOCK_STREAM, 0);
if (listen_sd < 0) // listen_sd = socket(AF_INET6, SOCK_STREAM | SOCK_NONBLOCK, 0);
{ if (listen_sd < 0)
perror("socket() failed"); {
exit(-1); perror("socket() failed");
} exit(-1);
}
/*************************************************************/ /*************************************************************/
/* Allow socket descriptor to be reuseable */ /* Allow socket descriptor to be reuseable */
/*************************************************************/ /*************************************************************/
rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
(char *)&on, sizeof(on)); if (rc < 0)
if (rc < 0) {
{ perror("setsockopt() failed");
perror("setsockopt() failed"); close(listen_sd);
close(listen_sd); exit(-1);
exit(-1); }
}
/*************************************************************/ /*************************************************************/
/* Set socket to be nonblocking. All of the sockets for */ /* Set socket to be nonblocking. All of the sockets for */
/* the incoming connections will also be nonblocking since */ /* the incoming connections will also be nonblocking since */
/* they will inherit that state from the listening socket. */ /* they will inherit that state from the listening socket. */
/*************************************************************/ /*************************************************************/
rc = ioctl(listen_sd, FIONBIO, (char *)&on); rc = ioctl(listen_sd, FIONBIO, (char *)&on);
if (rc < 0) if (rc < 0)
{ {
perror("ioctl() failed"); perror("ioctl() failed");
close(listen_sd); close(listen_sd);
exit(-1); exit(-1);
} }
/*************************************************************/ /*************************************************************/
/* Bind the socket */ /* Bind the socket */
/*************************************************************/ /*************************************************************/
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6; addr.sin6_family = AF_INET6;
memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any)); memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
addr.sin6_port = htons(SERVER_PORT); addr.sin6_port = htons(SERVER_PORT);
rc = bind(listen_sd, rc = bind(listen_sd,
(struct sockaddr *)&addr, sizeof(addr)); (struct sockaddr *)&addr, sizeof(addr));
if (rc < 0) if (rc < 0)
{ {
perror("bind() failed"); perror("bind() failed");
close(listen_sd); close(listen_sd);
exit(-1); exit(-1);
} }
/*************************************************************/ /*************************************************************/
/* Set the listen back log */ /* Set the listen back log */
/*************************************************************/ /*************************************************************/
rc = listen(listen_sd, 32); rc = listen(listen_sd, 32);
if (rc < 0) if (rc < 0)
{ {
perror("listen() failed"); perror("listen() failed");
close(listen_sd); close(listen_sd);
exit(-1); exit(-1);
} }
/*************************************************************/ /*************************************************************/
/* Initialize the master fd_set */ /* Initialize the master fd_set */
/*************************************************************/ /*************************************************************/
FD_ZERO(&master_set); FD_ZERO(&master_set);
max_sd = listen_sd; max_sd = listen_sd;
FD_SET(listen_sd, &master_set); FD_SET(listen_sd, &master_set);
/*************************************************************/ /*************************************************************/
/* Initialize the timeval struct to 3 minutes. If no */ /* Initialize the timeval struct to 3 minutes. If no */
/* activity after 3 minutes this program will end. */ /* activity after 3 minutes this program will end. */
/*************************************************************/ /*************************************************************/
timeout.tv_sec = 3 * 60; timeout.tv_sec = 3 * 60;
timeout.tv_usec = 0; timeout.tv_usec = 0;
/*************************************************************/ /*************************************************************/
/* Loop waiting for incoming connects or for incoming data */ /* Loop waiting for incoming connects or for incoming data */
/* on any of the connected sockets. */ /* on any of the connected sockets. */
/*************************************************************/ /*************************************************************/
do do
{ {
/**********************************************************/ /**********************************************************/
/* Copy the master fd_set over to the working fd_set. */ /* Copy the master fd_set over to the working fd_set. */
/**********************************************************/ /**********************************************************/
memcpy(&working_set, &master_set, sizeof(master_set)); memcpy(&working_set, &master_set, sizeof(master_set));
/**********************************************************/ /**********************************************************/
/* Call select() and wait 3 minutes for it to complete. */ /* Call select() and wait 3 minutes for it to complete. */
/**********************************************************/ /**********************************************************/
printf("Waiting on select()...\n"); printf("\nWaiting on select()...\n");
rc = select(max_sd + 1, &working_set, NULL, NULL, &timeout); rc = select(max_sd + 1, &working_set, NULL, NULL, &timeout);
/**********************************************************/ /**********************************************************/
/* Check to see if the select call failed. */ /* Check to see if the select call failed. */
/**********************************************************/ /**********************************************************/
if (rc < 0) if (rc < 0)
{ {
perror(" select() failed"); perror(" select() failed");
break; break;
} }
/**********************************************************/ /**********************************************************/
/* Check to see if the 3 minute time out expired. */ /* Check to see if the 3 minute time out expired. */
/**********************************************************/ /**********************************************************/
if (rc == 0) if (rc == 0)
{ {
printf(" select() timed out. End program.\n"); printf(" select() timed out. End program.\n");
break; break;
} }
/**********************************************************/ /**********************************************************/
/* One or more descriptors are readable. Need to */ /* One or more descriptors are readable. Need to */
/* determine which ones they are. */ /* determine which ones they are. */
/**********************************************************/ /**********************************************************/
desc_ready = rc; desc_ready = rc;
for (i=0; i <= max_sd && desc_ready > 0; ++i) for (i=0; i <= max_sd && desc_ready > 0; ++i)
{ {
/*******************************************************/ /*******************************************************/
/* Check to see if this descriptor is ready */ /* Check to see if this descriptor is ready */
/*******************************************************/ /*******************************************************/
if (FD_ISSET(i, &working_set)) if (FD_ISSET(i, &working_set))
{ {
/****************************************************/ /****************************************************/
/* A descriptor was found that was readable - one */ /* A descriptor was found that was readable - one */
/* less has to be looked for. This is being done */ /* less has to be looked for. This is being done */
/* so that we can stop looking at the working set */ /* so that we can stop looking at the working set */
/* once we have found all of the descriptors that */ /* once we have found all of the descriptors that */
/* were ready. */ /* were ready. */
/****************************************************/ /****************************************************/
desc_ready -= 1; desc_ready -= 1;
/****************************************************/ /****************************************************/
/* Check to see if this is the listening socket */ /* Check to see if this is the listening socket */
/****************************************************/ /****************************************************/
if (i == listen_sd) if (i == listen_sd)
{ {
printf(" Listening socket is readable\n"); printf("\n Listening socket is readable\n");
/*************************************************/ /*************************************************/
/* Accept all incoming connections that are */ /* Accept all incoming connections that are */
/* queued up on the listening socket before we */ /* queued up on the listening socket before we */
/* loop back and call select again. */ /* loop back and call select again. */
/*************************************************/ /*************************************************/
do do
{ {
/**********************************************/ /**********************************************/
/* Accept each incoming connection. If */ /* Accept each incoming connection. If */
/* accept fails with EWOULDBLOCK, then we */ /* accept fails with EWOULDBLOCK, then we */
/* have accepted all of them. Any other */ /* have accepted all of them. Any other */
/* failure on accept will cause us to end the */ /* failure on accept will cause us to end the */
/* server. */ /* server. */
/**********************************************/ /**********************************************/
new_sd = accept(listen_sd, NULL, NULL); new_sd = accept(listen_sd, NULL, NULL);
if (new_sd < 0) if (new_sd < 0)
{ {
if (errno != EWOULDBLOCK) if (errno != EWOULDBLOCK)
{ {
perror(" accept() failed"); perror(" accept() failed");
end_server = TRUE; end_server = TRUE;
} }
break; break;
} }
/**********************************************/ /**********************************************/
/* Add the new incoming connection to the */ /* Add the new incoming connection to the */
/* master read set */ /* master read set */
/**********************************************/ /**********************************************/
printf(" New incoming connection - %d\n", new_sd); printf(" New incoming connection - %d\n", new_sd);
FD_SET(new_sd, &master_set); FD_SET(new_sd, &master_set);
if (new_sd > max_sd) if (new_sd > max_sd)
max_sd = new_sd; max_sd = new_sd;
/**********************************************/ /**********************************************/
/* Loop back up and accept another incoming */ /* Loop back up and accept another incoming */
/* connection */ /* connection */
/**********************************************/ /**********************************************/
} while (new_sd != -1); } while (new_sd != -1);
} }
/****************************************************/ /****************************************************/
/* This is not the listening socket, therefore an */ /* This is not the listening socket, therefore an */
/* existing connection must be readable */ /* existing connection must be readable */
/****************************************************/ /****************************************************/
else else
{ {
printf(" Descriptor %d is readable\n", i); printf("\n Descriptor %d is readable\n", i);
close_conn = FALSE; close_conn = FALSE;
/*************************************************/ /*************************************************/
/* Receive all incoming data on this socket */ /* Receive all incoming data on this socket */
/* before we loop back and call select again. */ /* before we loop back and call select again. */
/*************************************************/ /*************************************************/
do do
{ {
/**********************************************/ /**********************************************/
/* Receive data on this connection until the */ /* Receive data on this connection until the */
/* recv fails with EWOULDBLOCK. If any other */ /* recv fails with EWOULDBLOCK. If any other */
/* failure occurs, we will close the */ /* failure occurs, we will close the */
/* connection. */ /* connection. */
/**********************************************/ /**********************************************/
rc = recv(i, buffer, sizeof(buffer), 0); rc = recv(i, buffer, sizeof(buffer), 0);
if (rc < 0) if (rc < 0)
{ {
if (errno != EWOULDBLOCK) if (errno != EWOULDBLOCK)
{ {
perror(" recv() failed"); perror(" recv() failed");
close_conn = TRUE; close_conn = TRUE;
} }
break; break;
} }
/**********************************************/ /**********************************************/
/* Check to see if the connection has been */ /* Check to see if the connection has been */
/* closed by the client */ /* closed by the client */
/**********************************************/ /**********************************************/
if (rc == 0) if (rc == 0)
{ {
printf(" Connection closed\n"); printf(" Connection closed\n");
close_conn = TRUE; close_conn = TRUE;
break; break;
} }
/**********************************************/ /**********************************************/
/* Data was received */ /* Data was received */
/**********************************************/ /**********************************************/
len = rc; len = rc;
printf(" %d bytes received\n", len); printf(" %d bytes received\n", len);
/**********************************************/ /**********************************************/
/* Echo the data back to the client */ /* Echo the data back to the client */
/**********************************************/ /**********************************************/
rc = send(i, buffer, len, 0); rc = send(i, buffer, len, 0);
if (rc < 0) if (rc < 0)
{ {
perror(" send() failed"); perror(" send() failed");
close_conn = TRUE; close_conn = TRUE;
break; break;
} }
} while (TRUE); } while (TRUE);
/*************************************************/ /*************************************************/
/* If the close_conn flag was turned on, we need */ /* If the close_conn flag was turned on, we need */
/* to clean up this active connection. This */ /* to clean up this active connection. This */
/* clean up process includes removing the */ /* clean up process includes removing the */
/* descriptor from the master set and */ /* descriptor from the master set and */
/* determining the new maximum descriptor value */ /* determining the new maximum descriptor value */
/* based on the bits that are still turned on in */ /* based on the bits that are still turned on in */
/* the master set. */ /* the master set. */
/*************************************************/ /*************************************************/
if (close_conn) if (close_conn)
{ {
close(i); close(i);
FD_CLR(i, &master_set); FD_CLR(i, &master_set);
if (i == max_sd) if (i == max_sd)
{ {
while (FD_ISSET(max_sd, &master_set) == FALSE) while (FD_ISSET(max_sd, &master_set) == FALSE)
max_sd -= 1; max_sd -= 1;
} }
} }
} /* End of existing connection is readable */ } /* End of existing connection is readable */
} /* End of if (FD_ISSET(i, &working_set)) */ } /* End of if (FD_ISSET(i, &working_set)) */
} /* End of loop through selectable descriptors */ } /* End of loop through selectable descriptors */
} while (end_server == FALSE); } while (end_server == FALSE);
/*************************************************************/ /*************************************************************/
/* Clean up all of the sockets that are open */ /* Clean up all of the sockets that are open */
/*************************************************************/ /*************************************************************/
for (i=0; i <= max_sd; ++i) for (i=0; i <= max_sd; ++i)
{ {
if (FD_ISSET(i, &master_set)) if (FD_ISSET(i, &master_set))
close(i); close(i);
} }
} }

BIN
webserv

Binary file not shown.