Fennet
Lightweight HTTP server library for C
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
9#include <fennet/lib.h>
10
11#ifndef _WIN32
12#include <arpa/inet.h>
13#include <netinet/in.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/socket.h>
18#include <unistd.h>
19#endif
20
21#define PORT 8080
22
26int
27main(int argc, char** argv)
28{
29#ifndef _WIN32
30 // Socket interface initialisation
31 int main_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
32 if (main_socket == -1) {
33 printf("Couldn't initialise the main socket.\n");
34 }
35 printf("Initialised the main socket!\n");
36
37 // The address we want to be bound as
38 struct sockaddr_in address = {
39 AF_INET, htons(PORT), { htonl(INADDR_ANY) }, {}
40 };
41
42 // Bind to the address
43 if (bind(main_socket, (const struct sockaddr*)&address, sizeof(address))) {
44 printf("Socket couldn't be bound.\n");
45 return 1;
46 }
47 printf("Socket bound!\n");
48
49 // Listen for new connections on socket
50 if (listen(main_socket, 15)) {
51 printf("Socket couldn't be listened on.\n");
52 return 1;
53 }
54 printf("Listening on socket!\n");
55
56 // Determine whether
57 struct sockaddr_in client_address = {};
58 unsigned int client_address_size = sizeof(client_address);
59 int connection = accept(
60 main_socket, (struct sockaddr*)&client_address, &client_address_size);
61 if (connection < 0) {
62 printf("Accept failed.\n");
63 }
64 printf("Client accepted!\n");
65
66 // Receive HTTP Request
67 char buf[1024 * 30] = {};
68 read(connection, buf, sizeof(buf));
69
70 // Send HTTP Response
71 const char* response = "HTTP/1.1 200 OK\n\n<h1>AAAAAAAAHELPME</h1>d";
72 write(connection, response, strlen(response));
73
74 shutdown(main_socket, SHUT_RDWR);
75 close(main_socket);
76#endif
77
78 return 0;
79}
int main(int argc, char **argv)
Entry point of the HelloClient Example.
Definition: main.c:27
Main library file.