-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
wserver.c
47 lines (36 loc) · 908 Bytes
/
wserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include "request.h"
#include "io_helper.h"
char default_root[] = ".";
//
// ./wserver [-d <basedir>] [-p <portnum>]
//
int main(int argc, char *argv[]) {
int c;
char *root_dir = default_root;
int port = 10000;
while ((c = getopt(argc, argv, "d:p:")) != -1)
switch (c) {
case 'd':
root_dir = optarg;
break;
case 'p':
port = atoi(optarg);
break;
default:
fprintf(stderr, "usage: wserver [-d basedir] [-p port]\n");
exit(1);
}
// run out of this directory
chdir_or_die(root_dir);
// now, get to work
int listen_fd = open_listen_fd_or_die(port);
while (1) {
struct sockaddr_in client_addr;
int client_len = sizeof(client_addr);
int conn_fd = accept_or_die(listen_fd, (sockaddr_t *) &client_addr, (socklen_t *) &client_len);
request_handle(conn_fd);
close_or_die(conn_fd);
}
return 0;
}