-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.c
63 lines (55 loc) · 1.59 KB
/
http_server.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
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8000
#define BUFFER_SIZE 1024
int main() {
// creating a socket
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) {
perror("webserver socket");
return 1;
}
printf("socket created successfully\n");
// creating address to bind with socket
struct sockaddr_in addr = {AF_INET, htons(PORT), INADDR_ANY};
if (bind(s, &addr, sizeof(addr)) != 0) {
perror("webserver (bind)");
return 1;
}
// listen for incoming connections
if (listen(s, 10) != 0) {
perror("webserver (listen)");
return 1;
}
printf("server listening for connections\n");
while (1) {
int client_fd = accept(s, NULL, NULL);
if (client_fd < 0) {
perror("webserver (accept)");
continue;
}
printf("connection accepted\n");
char buffer[BUFFER_SIZE] = {0};
recv(client_fd, buffer, BUFFER_SIZE, 0);
char *f = buffer + 5;
*strchr(f, ' ') = 0;
const char *message =
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
"<!DOCTYPE html><html><head><title>ABHAY PATEL IS A "
"GENIUS!</title></head>"
"<body><h1>you heared it right!</h1><br><h1>Abhay "
"Patel is the next big thing</h1></body></html>"
"<br><h1>Abhay Patel is the next big thing</h1></body></html>"
"<br><h1>Abhay Patel is the next big thing</h1></body></html>";
send(client_fd, message, strlen(message), 0);
close(client_fd);
}
close(s);
return 0;
}