Boost Asio type to use for both unix-socket and tcp socket

ぐ巨炮叔叔 提交于 2020-01-01 03:29:08

问题


We have a boost asio based networking code, which connects to a remote side. The local side could be either a tcp4 socket or a unix socket.

Is there a typename to use that could hold both of these type of boost sockets? (e.g. something like a base class for both?). Currently our code use boost::asio::generic::stream_protocol::socket for tcp socket, and boost::asio::local::stream_protocol::socket for a unix socket.


回答1:


Actually, there's a dedicated ip::tcp::socket type for tcp sockets. As for generic::stream_protocol::socket, it is the universal stream socket type that accepts socket protocol and family in run-time, so you can use it for the both types you need:

generic::stream_protocol::socket ipc(io_, generic::stream_protocol(AF_UNIX, 0));
generic::stream_protocol::socket tcp(io_, generic::stream_protocol(AF_INET, IPPROTO_TCP));


来源:https://stackoverflow.com/questions/19123980/boost-asio-type-to-use-for-both-unix-socket-and-tcp-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!