C++ boost unix domain socket using send_to

boost-asio, c++, ipc

Solution

You've never opened the socket by calling `open()`, or any of the other methods that open it automatically, such as `connect()`.

int
main(void)
{
    const pid_t pid = getpid();
    boost::asio::io_service my_service;
    const boost::asio::local::datagram_protocol::endpoint ep("/tmp/socketname");
    boost::asio::local::datagram_protocol::socket my_sock(my_service);

    //my_sock.connect(ep);
    //my_sock.send(boost::asio::buffer(&pid, sizeof(pid)));
    my_sock.open();
    my_sock.send_to(boost::asio::buffer(&pid, sizeof(pid)), ep);

    return 0;
}

Problem

I am trying to write a very simple unix domain datagram client/server. Here is the python server: ``` import socket,os s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) try: os.remove("/tmp/socketname") except OSError: pass s.bind("/tmp/socketname") while 1: data = s.recv(1024) print data conn.close() ``` and here is a python client that seems to work just fine with the server ``` import socket import time sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) sock.sendto('Hello, world', "/tmp/socketname") sock.close() ``` The purpose of this exercise was to play with boost::asio networking library, the python code just makes the server simple with a simple client to prove(-ish) that the server is working. I am having some issues with the C++ client: ``` #include <boost/asio.hpp> int main(void) { const pid_t pid = getpid(); boost::asio::io_service my_service; const boost::asio::local::datagram_protocol::endpoint ep("/tmp/socketname"); boost::asio::local::datagram_protocol::socket my_sock(my_service); //my_sock.connect(ep); //my_sock.send(boost::asio::buffer(&pid, sizeof(pid))); my_sock.send_to(boost::asio::buffer(&pid, sizeof(pid)), ep); return 0; } ``` If I comment out the 2 lines that use connect/send the seems to work. But the the line which uses send_to fails with the following error. terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): Bad file descriptor Aborted Thanks for any and all help provided.

Original source