int xuj struct sockaddr_in addr client_addr addr sin_family AF_INET ad

 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
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 SO_REUSEADDR!");
return 1;
}
flag = true;
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) != 0) {
perror("Can't set socket option TCP_NODELAY!");
return 1;
}
if (bind(sock, (struct sockaddr *)&(addr), sizeof(addr)) != 0) {
perror("Can't bind socket!");
return 1;
}
if (listen(sock, 1) != 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\
Connection: close\r\n\
\r\n\
<div>XUJ PIZD JGURDA SERVER</div>\n\
";
char buffer[1024 + 1];
int client;
int readed;
socklen_t client_addr_len = sizeof(client_addr);
flag = 1;
while ((client = accept(sock, (struct sockaddr *)&client_addr, &client_addr_len)) >= 0) {
setsockopt(client, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
//while ((readed = recv(client, &buffer, sizeof(buffer) - 1, MSG_DONTWAIT)) > 0) {
// buffer[readed] = 0;
// printf("%s", buffer);
//}
// shutdown(client, SHUT_RD);
// send(client, html, sizeof(html) - 1, MSG_WAITALL);
// shutdown(client, SHUT_WR);
shutdown(client, SHUT_RDWR);
close(client);
}
shutdown(sock, SHUT_RDWR);
close(sock);
return 0;
}