简体   繁体   English

如何使用 boost::asio 连接到 unix 域套接字?

[英]How to connect to an unix domain socket with boost::asio?

I want to create and connect to an unix domain socket of type SOCK_SEQPACKET by specifying the path name of the socket endpoint, but this fails to compile in boost::asio v1.60:我想通过指定套接字端点的路径名来创建并连接到SOCK_SEQPACKET类型的unix domain socket ,但这无法在boost::asio v1.60 中编译:

using namespace boost::asio::generic;
seq_packet_protocol proto{AF_UNIX, IPPROTO_SCTP}; // SOCK_SEQPACKET
seq_packet_protocol::socket sock(io_service, proto);
boost::asio::local::basic_endpoint<seq_packet_protocol> ep("/tmp/socket");
sock.connect(ep); // does not compile

do you know how to properly create an unix domain socket?你知道如何正确创建一个unix域套接字吗?

I suggest to keep it simple:我建议保持简单:

#include <boost/asio.hpp>

int main() {
    boost::asio::io_service io_service;
    using boost::asio::local::stream_protocol;

    stream_protocol::socket s(io_service);
    s.connect("/tmp/socket");
}

No doubt you can go more lowlevel, but I'm not sure when you'd need that.毫无疑问,您可以进入更底层,但我不确定您何时需要它。

UPDATE Mimicking the pre-defined stream_protocol , here's how to define seqpacket_protocol :更新模仿预定义的stream_protocol ,这里是如何定义seqpacket_protocol

Live On Coliru 住在 Coliru

namespace SeqPacket {
    using namespace boost::asio::local;

    struct seqpacket_protocol
    {
        int type()     const { return IPPROTO_SCTP; }
        int protocol() const { return 0;            }
        int family()   const { return AF_UNIX;      }

        typedef basic_endpoint<seqpacket_protocol> endpoint;
        typedef boost::asio::basic_stream_socket<seqpacket_protocol> socket;
        typedef boost::asio::basic_socket_acceptor<seqpacket_protocol> acceptor;

#if !defined(BOOST_ASIO_NO_IOSTREAM)
        /// The UNIX domain iostream type.
        typedef boost::asio::basic_socket_iostream<seqpacket_protocol> iostream;
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
    };
}

Just use it in the same pattern:只需以相同的模式使用它:

int main() {
    boost::asio::io_service io_service;
    using SeqPacket::seqpacket_protocol;

    seqpacket_protocol::socket s(io_service);
    s.connect("socket");
}

To add on to sehe's answer, the socket definition is wrong, it should be:要补充sehe的答案,套接字定义是错误的,应该是:

typedef boost::asio::basic_seq_packet_socket<seqpacket_protocol> socket;

putting it together:把它放在一起:

namespace SeqPacket {
    struct seqpacket_protocol
    {
        int type()     const { return SOCK_SEQPACKET; }
        int protocol() const { return 0;            }
        int family()   const { return AF_UNIX;      }

        typedef boost::asio::local::basic_endpoint<seqpacket_protocol> endpoint;
        typedef boost::asio::basic_seq_packet_socket<seqpacket_protocol> socket;
        typedef boost::asio::basic_socket_acceptor<seqpacket_protocol> acceptor;

#if !defined(BOOST_ASIO_NO_IOSTREAM)
        /// The UNIX domain iostream type.
        typedef boost::asio::basic_socket_iostream<seqpacket_protocol> iostream;
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM