Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
remzi-arpacidusseau
GitHub Repository: remzi-arpacidusseau/ostep-projects
Path: blob/master/concurrency-webserver/src/wserver.c
909 views
1
#include <stdio.h>
2
#include "request.h"
3
#include "io_helper.h"
4
5
char default_root[] = ".";
6
7
//
8
// ./wserver [-d <basedir>] [-p <portnum>]
9
//
10
int main(int argc, char *argv[]) {
11
int c;
12
char *root_dir = default_root;
13
int port = 10000;
14
15
while ((c = getopt(argc, argv, "d:p:")) != -1)
16
switch (c) {
17
case 'd':
18
root_dir = optarg;
19
break;
20
case 'p':
21
port = atoi(optarg);
22
break;
23
default:
24
fprintf(stderr, "usage: wserver [-d basedir] [-p port]\n");
25
exit(1);
26
}
27
28
// run out of this directory
29
chdir_or_die(root_dir);
30
31
// now, get to work
32
int listen_fd = open_listen_fd_or_die(port);
33
while (1) {
34
struct sockaddr_in client_addr;
35
int client_len = sizeof(client_addr);
36
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
37
request_handle(conn_fd);
38
close_or_die(conn_fd);
39
}
40
return 0;
41
}
42
43
44
45
46
47
48
49