38 lines
961 B
Plaintext
38 lines
961 B
Plaintext
|
|
|
||
|
|
int main() {
|
||
|
|
int server_fd;
|
||
|
|
int client_fd;
|
||
|
|
char buffer[4096];
|
||
|
|
char *response;
|
||
|
|
int bytes_received;
|
||
|
|
int response_len;
|
||
|
|
int count;
|
||
|
|
|
||
|
|
server_fd = socket(AF_INET(), SOCK_STREAM(), 0);
|
||
|
|
bind(server_fd, 8080);
|
||
|
|
listen(server_fd, 10);
|
||
|
|
|
||
|
|
printf("Server listening on port 8080\n");
|
||
|
|
|
||
|
|
count = 1;
|
||
|
|
while (count > 0) {
|
||
|
|
printf("Waiting for connection %d\n", count);
|
||
|
|
client_fd = accept(server_fd, 0, 0);
|
||
|
|
printf("Client connected\n");
|
||
|
|
|
||
|
|
bytes_received = recv(client_fd, buffer, 4096, 0);
|
||
|
|
printf("Received %d bytes\n", bytes_received);
|
||
|
|
|
||
|
|
response = "HTTP/1.1 200 OK\nContent-Type: text/html\n\n<html><body><h1>Hello from Mini C!</h1><p>Request number: ";
|
||
|
|
response_len = strlen(response);
|
||
|
|
send(client_fd, response, response_len, 0);
|
||
|
|
|
||
|
|
close(client_fd);
|
||
|
|
printf("Connection closed\n");
|
||
|
|
count = count + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
close(server_fd);
|
||
|
|
return 0;
|
||
|
|
}
|