Martin
Martin

Reputation: 9369

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:

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?

Upvotes: 8

Views: 9276

Answers (2)

sehe
sehe

Reputation: 393354

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 2 In Boost 1.82.0, Asio 1.27.0 added local::seq_packet_protocol

UPDATE Mimicking the pre-defined stream_protocol, here's how to define seqpacket_protocol:

Live On 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");
}

Upvotes: 6

Hiroki Takeuchi
Hiroki Takeuchi

Reputation: 1

To add on to sehe's answer, the socket definition is wrong, it should be:

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

Upvotes: 0

Related Questions