-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path12lineweb.cpp
36 lines (26 loc) · 887 Bytes
/
12lineweb.cpp
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
#include <cstdlib>
#include <thread>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main() {
int sock = ::socket(AF_INET, SOCK_STREAM, 0);
::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, new int(1), sizeof(int));
sockaddr_in serv_addr = {AF_INET, htons(8989), INADDR_ANY};
::bind(sock, (sockaddr *) &serv_addr, sizeof(serv_addr));
::listen(sock, 5);
do {
sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
int in = ::accept(sock, reinterpret_cast<sockaddr*>(&cli_addr), &clilen);
std::thread([&in]() {
int r = ::send(in, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\nHello there.", 62, 0);
::close(in);
}).detach();
} while(true);
}