在下一节课中,我们将回顾一个更大的服务器示例 — TCP聊天服务器。在跳入那部分之前,我们首先应该学习一些新的内容。
关于套接字的一个新知识点是,我们可以请求它在连接的两端的端点:
boost::asio::ip::tcp::endpoint endpoint;
endpoint = socket.local_endpoint(); // 本地连接端的IP和端口
endpoint = socket.remote_endpoint(); // 远程连接端的IP和端口
请注意,这些函数可能会抛出异常。如果您不想处理异常,可以使用返回值重载:
boost::system:error_code error;
auto endpoint = socket.remote_endpoint(error);
`endpoint` 可以与iostreams一起使用:
boost::system:error_code error;
auto endpoint = socket.remote_endpoint(error);
std::cout << "Remote endpoint: " << endpoint << "\n";
Remote endpoint: 127.0.0.1:38529
有时您需要取消之前安排的异步操
本文介绍了Boost.Asio库中关于套接字的高级操作,包括获取连接端点、异步操作的取消与关闭、streambuf的使用以及异步操作的队列管理。此外,还探讨了服务器与会话对象之间的通信策略,为构建TCP聊天服务器打下基础。
订阅专栏 解锁全文
2597

被折叠的 条评论
为什么被折叠?



