[erlang-questions] General help with unix sockets

Michael Santos michael.santos@REDACTED
Thu Jun 9 15:03:43 CEST 2011

On Thu, Jun 09, 2011 at 12:19:42PM +0100, Paul G Webster wrote:
> Good day,
> I was wondering if anyone could lend a hand; I am trying to use
> erlang to connect to a unix socket '/var/pexim.sock' I spied out
> 'procket' it looks good but I am a little to new to erlang to figure
> out howto actually use the toolkit to connect..
> Would anyone be able to give me a hand; it does not even need to be
> procket .. just a simple connection script for a unix socket
> If you could help it would be excellent
> p.s. If this actually ends up on the list twice; my apologies :)

There are a few ways to connect to Unix sockets from Erlang. I responded
to your other email privately but for the archives my blog has some info
about it:


Assuming that /var/pexim.sock is a stream socket (is that path right?) and
you're using procket, the steps are similar to opening a socket in C.
The layout of a struct sockaddr_un will depend on your OS. For example,
it's defined in /usr/include/linux/un.h as:

    #define UNIX_PATH_MAX   108
    struct sockaddr_un {
        sa_family_t sun_family; /* AF_UNIX */
        char sun_path[UNIX_PATH_MAX];   /* pathname */

So the code to open the socket would look something like (untested):

    Path = <<"/var/pexim.sock">>,
    PF_LOCAL = 1,

    % Get a Unix stream socket fd
    {ok, Socket} = procket:socket(local, stream, 0),
    Len = 108 - byte_size(Path),

    % Linux sockaddr_un
    Sun = <<PF_LOCAL/native,

    ok = procket:connect(Socket, Sun),

    {ok, Buf} = procket:read(Socket, 16#FFFF),
    ok = procket:write(Socket, Buf),

    ok = procket:close().

I've left out the portability stuff for running on both BSD/Linux.

Hope that helps!

More information about the erlang-questions mailing list