Repository navigation
Expand file tree
/
Copy pathserver.c
More file actions
60 lines (47 loc) · 1.4 KB
/
Copy pathserver.c
File metadata and controls
60 lines (47 loc) · 1.4 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Socket creation failed");
return 1;
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(8080);
address.sin_addr.s_addr = INADDR_ANY;
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
return 1;
}
if (listen(server_fd, 10) <0) {
perror("Listen failed");
close(server_fd);
return 1;
}
printf("Server listening on http://localhost:8080...\n");
printf("Waiting for a client to connect...\n");
int client_fd = accept(server_fd, NULL, NULL);
if (client_fd < 0) {
perror("Accept failed");
close(server_fd);
return 1;
}
printf("Client connected\n");
char buffer[1024] = {0};
read(client_fd, buffer, sizeof(buffer) -1);
printf("---Incoming Request---\n%s\n----------\n", buffer);
char *http_response =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>Hello from my C Web Server!</h1>";
write(client_fd, http_response, strlen(http_response));
close(client_fd);
close(server_fd);
return 0;
}