-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.c
46 lines (34 loc) · 1.33 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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main(){
FILE *html_data;
//Opening the html file
html_data = fopen("index.html", "r");
//Read the response from the html file
char response_data[1024];
fgets(response_data, 1024, html_data);
char http_header[2048] = "HTTP/1.1 200 OK\r\n\n";
strcat(http_header, response_data);
//Creating a socket
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
//Defining the address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(3000);
server_address.sin_addr.s_addr = INADDR_ANY;
//Binding
bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
//Listening
listen(server_socket, 5);
int client_socket;
while(1) {
client_socket = accept(server_socket, NULL, NULL);
send(client_socket, http_header, sizeof(http_header), 0);
close(client_socket);
}
return 0;
}