#include <iostream> #include <experimental/net> #include <array> #include <system_error> int main() { using namespace std::experimental; using net::ip::tcp; net::io_context io; tcp::acceptor acceptor(io,tcp::endpoint(tcp::v4(), 3954) ); std::array<char,128> arr; std::string s{"please wait"}; for (;;) { try { std::error_code err; tcp::socket socket = acceptor.accept(io,err); size_t len = socket.read_some(net::buffer(arr), err); std::cout << "read " << len << " letters" << std::endl; for (size_t i = 0; i < len; ++i) { std::cout << arr[i]; } socket.write_some(net::buffer(s),err ); socket.close(); } catch (std::exception &e) { std::cerr << e.what() << std::endl; } } return 0; }
编译:
g++ -o test test.cpp
原文:https://www.cnblogs.com/limancx/p/14794517.html