mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
add poll to handle user input and web requests at the same time
This commit is contained in:
34
webserver.c
34
webserver.c
@@ -1,9 +1,12 @@
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "client_handler.h"
|
||||
|
||||
@@ -18,7 +21,6 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
// Socket address config
|
||||
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(8080);
|
||||
@@ -36,12 +38,36 @@ int main(int argc, char **argv) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Set up poll to monitor stdin and server
|
||||
struct pollfd fds[2];
|
||||
fds[0].fd = STDIN_FILENO;
|
||||
fds[0].events = POLLIN;
|
||||
fds[1].fd = server;
|
||||
fds[1].events = POLLIN;
|
||||
|
||||
// Main loop
|
||||
while (1) {
|
||||
int ret = poll(fds, 2, 0);
|
||||
if (ret > 0) {
|
||||
// Check if stdin has data
|
||||
if (fds[0].revents & POLLIN) {
|
||||
char c;
|
||||
read(STDIN_FILENO, &c, 1);
|
||||
if (c == 'q') {
|
||||
close(server);
|
||||
printf("Server closed\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if server has data
|
||||
if (fds[1].revents & POLLIN) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
int *client = malloc(sizeof(int));
|
||||
|
||||
*client = accept(server, (struct sockaddr *)&client_addr, &client_len);
|
||||
*client = accept(server, (struct sockaddr *)&client_addr,
|
||||
&client_len);
|
||||
if (client < 0) {
|
||||
perror("Failed to accept client");
|
||||
continue;
|
||||
@@ -51,4 +77,8 @@ int main(int argc, char **argv) {
|
||||
pthread_create(&thread, NULL, client_handler, (void *)client);
|
||||
pthread_detach(thread);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user