asio tcp client and server implement
tcp server
#include <cstdlib> #include <iostream> #include <thread> #include <utility> #include "asio.hpp" using asio::ip::tcp; const int max_length = 1024; void session(tcp::socket sock) { try { for (;;) { char data[max_length]; asio::error_code error; size_t length = sock.read_some(asio::buffer(data), error); if (error == asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw asio::system_error(error); // Some other error. char data_print[max_length]; strncpy(data_print, data, length); data_print[length] = '