[erlang-questions] General help with unix sockets

Paul G Webster opensource@REDACTED
Thu Jun 9 20:23:07 CEST 2011


Out of interest,

my exim socket is working wonderfully but is it normal for it to  
immediately disconnect:

server app:

> ./ss
Accepted connection! at ./ss line 51.
GOT: test at ./ss line 86.
Server session encountered read error 0: Normal disconnection.

erlang app:

> erl -pa procket/ebin/
Erlang R14B03 (erts-5.8.4) [source] [64-bit] [smp:2:2] [rq:2]  
[async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.4  (abort with ^G)
1> c(unix_stream).
{ok,unix_stream}
2> unix_stream:client(<<"test\n">>).
<<"tset\r\n">>

telnet -u:

> telnet -u /tmp/testsocket
Trying /tmp/testsocket...
Connected to /tmp/testsocket.
Escape character is '^]'.
test
tset
test2
2tset


My would preffer the socket did not exit immediately after it got a result  
I am hoping that I can run a TCP server in exim that will accept multiple  
connections and kind of relay to the unix socket selective data from the  
multiple TCP connections. But obviously I will not be able to do that if  
ever request is automatically terminated :) well.. I bet I could but it  
would spam the unix sock


On Thu, 09 Jun 2011 13:03:43 -0000, Michael Santos  
<michael.santos@REDACTED> wrote:

> 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:
>
> http://listincomprehension.com/2010/12/unix-sockets.html
>
> 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,
>             Path/binary,
>             0:(Len*8)>>,
>
>     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!


-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/



More information about the erlang-questions mailing list