Question

Is there support for creating Unix domain sockets with SOCK_SEQPACKET parameter in boost asio? I didn't found any in the official reference, but maybe there are workarounds?

Was it helpful?

Solution 3

Well, I found a thing, that had been added in boost 1.54.0. It is called generic::seq_packet_protocol. When initialized as seq_packet_protocol(AF_UNIX, 0)) it produces an object, that can be used to open sockets, create acceptors and endpoints. So, SCTP could be implemented the same way.

OTHER TIPS

You should look at boost::asio::seq_packet_socket_service,

template<typename Protocol>
class seq_packet_socket_service : public io_service::service

See more at http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/seq_packet_socket_service.html

The answer pointed me into the right direction. Here is a more complete example that worked for me. It is the server part using standalone asio and C++17's std::filesystem to remove the old file.

#include <asio.hpp>

#include <array>
#include <filesystem>
#include <string>

int main() {
    asio::io_context io_context;
    std::error_code ec;
    std::array<char, 8192> buffer{};
    const std::string socket_address{"://tmp/sock.uds"};
    std::filesystem::remove(socket_address.substr(2, socket_address.size() - 2), ec);
    std::cout << "Removing " << socket_address << "; " << ec.message() << "\n";

    asio::generic::seq_packet_protocol::endpoint ep{socket_address.c_str(), socket_address.size()};
    ep.data()->sa_family = AF_LOCAL;
    asio::basic_socket_acceptor<asio::generic::seq_packet_protocol, asio::io_context::executor_type> acceptor(io_context);
    acceptor.open(ep.protocol(), ec);
    acceptor.bind(ep, ec);
    acceptor.listen(1);
    auto socket = acceptor.accept(ec);

    asio::socket_base::message_flags out_flags;

    std::cout << "Connection accepted";
    for (;;) {
        socket.receive(asio::buffer(buffer, buffer.size()), out_flags);
        std::cout << ec.message() << "\n";
        std::cout << buffer.data() << "\n";
    }
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top