Fennet
Lightweight HTTP server library for C
Main Page
Files
File List
Globals
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
26
int
27
main
(
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
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
}
main
int main(int argc, char **argv)
Entry point of the HelloClient Example.
Definition:
main.c:27
lib.h
Main library file.
examples
HelloClient
main.c
Generated by
1.9.5