-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
85 lines (69 loc) · 2.23 KB
/
main.c
File metadata and controls
85 lines (69 loc) · 2.23 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
#define PORT 8080
int main()
{
struct pollfd socket_poll;
struct sockaddr_in sl_address, cl_address;
int http_socketfd, clientfd;
int nfds = 0;
socklen_t cl_address_len = sizeof(cl_address);
memset(&sl_address, 0, sizeof(sl_address));
sl_address.sin_family = AF_INET;
sl_address.sin_port = htons(PORT);
sl_address.sin_addr.s_addr = INADDR_ANY;
memset(&cl_address, 0, sizeof(cl_address));
printf("Creating socket...\n");
http_socketfd = socket(AF_INET, SOCK_STREAM, 0);
nfds = 1;
if(http_socketfd == -1)
{
printf("Creating the socket failed\n");
return 1;
}
socket_poll.fd = http_socketfd;
socket_poll.events = POLLIN;
sigset_t signal_mask;
sigemptyset(&signal_mask);
printf("Binding socket...\n");
if(bind(http_socketfd, (struct sockaddr *) &sl_address, sizeof(sl_address)) == -1)
{
close(http_socketfd);
printf("Binding socket failed\n");
return 1;
};
printf("Socket successfully created and bound.\n");
listen(http_socketfd, 1);
printf("Socket listening on port %d\n", PORT);
while(nfds >= 1)
{
int poll_count = poll(&socket_poll, nfds, 50);
if(poll_count == -1)
{
printf("Error during polling socket input events\n");
nfds = 0;
break;
}
if(socket_poll.revents & POLLIN)
{
printf("Socket polled due to incoming event\n");
clientfd = accept(http_socketfd, (struct sockaddr *) &cl_address, &cl_address_len);
if(clientfd == -1)
{
printf("Opening client socket file descriptor failed.\n");
}
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &cl_address.sin_addr, client_ip, sizeof(client_ip));
printf("Client from address %s has connected to the socket.\n", client_ip);
close(clientfd);
}
}
close(http_socketfd);
return 0;
}