-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.c
75 lines (57 loc) · 1.84 KB
/
webserver.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#define APP_MAX_BUFFER 1024
#define PORT 8080
int main() {
// Server and Client File Descriptors
int server_fd, client_fd;
// socked address
struct sockaddr_in address;
int address_len = sizeof(address);
// Buffer array to store client data
// copy data from kernel
char buffer[APP_MAX_BUFFER] = {0};
// Server Socked
// SOCK_STREAM -> TCP
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socked creating failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET; // IPv4
address.sin_addr.s_addr = INADDR_ANY; // 0.0.0.0
address.sin_port = htons(PORT);
if (bind(server_fd, (const struct sockaddr *)&address, address_len) < 0) {
perror("Faild to bind socked and address");
exit(EXIT_FAILURE);
}
// Creates the queues
// Listen for clients, with 10 backlog (10 connection in accept queue)
if (listen(server_fd, 10) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
while (1) {
printf("\nWaiting for a connection...\n");
// Accept a client connection
if ((client_fd = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)(&address_len))) < 0) {
perror("Accpet failed");
exit(EXIT_FAILURE);
}
// read data from OS receive buffer to the application (buffer)
read(client_fd, buffer, APP_MAX_BUFFER);
printf("%s\n", buffer);
char *http_response = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 7\r\n" // more bytes then data
"\r\n"
"Hello\r\n";
write(client_fd, http_response, strlen(http_response));
close(client_fd);
}
return 0;
}