-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.cpp
38 lines (29 loc) · 903 Bytes
/
server.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
35
36
37
38
#include <array>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main()
{
boost::asio::io_service io_service;
tcp::acceptor acc(io_service, tcp::endpoint(tcp::v6(), 9876));
while (1) {
boost::system::error_code ignored;
tcp::socket socket(io_service);
acc.accept(socket);
std::array<char, 256> input_buffer;
std::size_t input_size = socket.read_some(
boost::asio::buffer(input_buffer),
ignored);
std::string visitor(input_buffer.data(),
input_buffer.data() + input_size);
std::cout << "Visited from " +
socket.remote_endpoint().address().to_string() +
" by visitor " << visitor << std::endl;
std::string message = "Hello, you are " + visitor + "\n";
boost::asio::write(socket, boost::asio::buffer(message), ignored);
socket.shutdown(tcp::socket::shutdown_both, ignored);
socket.close();
}
return 0;
}