Boost Asio类型可用于unix-socket和tcp套接字 [英] Boost Asio type to use for both unix-socket and tcp socket

查看:702
本文介绍了Boost Asio类型可用于unix-socket和tcp套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于boost asio的联网代码,该代码连接到远程端. 本地端可以是tcp4套接字,也可以是unix套接字.

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.

是否有一个可以同时使用这两种类型的Boost插座的类型名? (例如,两者都类似于基类?). 目前我们的代码使用 boost::asio::generic::stream_protocol::socket用于tcp套接字,并且 boost::asio::local::stream_protocol::socket用于unix套接字.

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.

推荐答案

实际上,tcp套接字有专用的ip::tcp::socket类型. 对于generic::stream_protocol::socket,它是通用流套接字类型,它在运行时接受套接字协议和家族,因此您可以将其用于所需的两种类型:

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));

这篇关于Boost Asio类型可用于unix-socket和tcp套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆