Search code examples
c++socketslibuv

LibUV create UDP stream from existing socket


I know very little about how unix works, so please excuse my ignorance.

I have a path to a socket on my linux box, /data/sock/socket.

I want to send data to that socket with LibUV, but I am unsure how to go about that.

I currently have it set up to create its own socket,

uv_udp_t m_socket;
uv_udp_send_t m_send_req;
sockaddr_in m_addr;

uv_udp_init(uv_default_loop(), &m_socket);
uv_ip4_addr("0.0.0.0", 8008, &m_addr);

char buff[6] = "Hello\0";
auto buf = uv_buf_init(buff, 6);
uv_udp_send(&m_send_req, &m_socket, &buf, 1, (const struct sockaddr*)&m_addr, NULL);

How can I use the socket I have on my filesystem, rather than using the one created through code?


Solution

  • Those sockets are called "local domain" sockets, and not UDP sockets. Fortunately, LibUV does support those (only on UNIX, obviously; although on Windows the same LibUV API use "Named Pipes" under the hood.)

    Anyways... In short, you have to use the pipe API (i.e. uv_pipe_init and uv_pipe_bind()/uv_pipe_connect,) instead of uv_udp_* functions.

    You can find the documentation for LibUV's pipe API here.