Fennet
Lightweight HTTP server library for C
Loading...
Searching...
No Matches
main.c File Reference

HelloClient example. More...

#include <fennet/lib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define PORT   8080
 

Functions

int main (int argc, char **argv)
 Entry point of the HelloClient Example. More...
 

Detailed Description

HelloClient example.

HelloServer is a simple example of fennet's usage, it connects to an HTTP server and prints it's response.

Definition in file main.c.

Macro Definition Documentation

◆ PORT

#define PORT   8080

Definition at line 21 of file main.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Entry point of the HelloClient Example.

 * 

Definition at line 27 of file main.c.

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 return 1;
35 }
36 printf("Initialised the main socket!\n");
37
38 // The address we want to connect to
39 struct sockaddr_in address = {
40 AF_INET, htons(PORT), { inet_addr("127.0.0.1") }, {}
41 };
42
43 // Connect to the address (will return other than 0 if it failed)
44 if (connect(main_socket, (const struct sockaddr*)&address, sizeof(address))) {
45 printf("Connection failed.\n");
46 return 1;
47 }
48 printf("Connected to the server successfully!\n");
49
50 // Send HTTP request message to the sever
51 const char* msg = "GET / HTTP/1.1\n\n";
52 write(main_socket, msg, strlen(msg));
53
54 // Receive HTTP response
55 char ret[1024 * 30] = {};
56 read(main_socket, ret, sizeof(ret));
57 printf("\nServer's reply:\n%s\n", ret);
58
59 close(main_socket);
60#endif
61
62 return 0;
63}