include cstdio include cstdlib include unistd include sys types includ

 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
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
int xuj() {
struct sockaddr_in addr, client_addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1488);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Can't open socket!");
return 1;
}
int flag = true;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) != 0) {
perror("Can't set socket option!");
return 1;
}
if (bind(sock, (struct sockaddr *)&(addr), sizeof(addr)) != 0) {
perror("Can't bind socket!");
return 1;
}
if (listen(sock, 4) != 0) {
perror("Can't listen socket!");
return 1;
}
static const char html[] = "HTTP/1.1 404 Not Found\r\n\
Content-Type: text/html\r\n\
Host: spaces.ru\r\n\
\r\n\
<div>XUJ PIZD JGURDA SERVER</div>\n\
";
int client;
socklen_t client_addr_len = sizeof(client_addr);
while ((client = accept(sock, (struct sockaddr *)&client_addr, &client_addr_len)) >= 0) {
shutdown(client, SHUT_RD);
send(client, html, sizeof(html) - 1, 0);
shutdown(client, SHUT_WR);
close(client);
}
shutdown(sock, SHUT_RDWR);
close(sock);
return 0;
}
int main() {
return xuj();
}