I have built a very naive parallel ssl acceptor.
-module(multiserver).
-export([start/0,client/1]).
%% This is a dummy SSL Erlang server/client example
start() ->
spawn_link(fun() -> init([]) end).
init([]) ->
ssl:start(),
{ok, ListenSocket} = ssl:listen(9990, [{certfile, "cert.pem"}, {keyfile, "privkey.pem"} ,{reuseaddr, true},{active, true}, binary]),
Pid = self(),
spawn_link(fun() -> listener(ListenSocket, Pid, 1) end),
spawn_link(fun() -> listener(ListenSocket, Pid, 2) end),
loop().
loop() ->
receive
{new, _Pid} ->
%% Do stuff here
loop()
end.
listener(ListenSocket, Pid, Num) ->
{ok, ClientSocket} = ssl:transport_accept(ListenSocket),
ok = ssl:ssl_accept(ClientSocket),
io:format("listener ~p accepted ~n", [Num]),
ok = ssl:send(ClientSocket, "server"),
io:format("listener ~p sent~n", [Num]),
receive
X -> io:format("listener ~p: ~p ~n", [Num, X])
after 5000 ->
io:format("listener ~p timeout ~n", [Num]),
timeout
end,
ssl:close(ClientSocket),
listener(ListenSocket, Pid, Num).
client(Message) ->
ssl:start(),
{ok, Socket} = ssl:connect("localhost", 9990, [binary, {active,true}], infinity),
receive
X -> io:format("~p ~n", [X])
after 2000 ->
timeout
end,
ok = ssl:send(Socket, Message),
ssl:close(Socket),
io:format("client closed~n").
The probelm I have is that listener 2 does not seem to be able to receive any messages. A sample run of the program looks like this:
First I start the server in shell 1.
Shell 1:
1> multiserver:start().
<0.34.0>
Then I call the client/1 three times from a different shell.
Shell 2:
2> multiserver:client("client").
{ssl,{sslsocket,new_ssl,<0.51.0>},<<"server">>}
client closed
ok
3> multiserver:client("client").
{ssl,{sslsocket,new_ssl,<0.54.0>},<<"server">>}
client closed
ok
4> multiserver:client("client").
{ssl,{sslsocket,new_ssl,<0.56.0>},<<"server">>}
client closed
ok
This is the printouts to server shell.
Shell 1:
listener 1 accepted
listener 1 sent
listener 1: {ssl,{sslsocket,new_ssl,<0.51.0>},<<"client">>}
listener 2 accepted
listener 2 sent
listener 1 accepted
listener 1 sent
listener 1: {ssl,{sslsocket,new_ssl,<0.54.0>},<<"client">>}
listener 2 timedout
2>
I have spent some hours with this and I cant understand why it is not possible for listener 2 to receive any data. If I edit the code to use gen_tcp it works as expected.
Is there something I am missing?
Is it possible to do this with the current ssl module?
The reason for the timeout is that in the second process uses the socket option {active,false}, i.e. the receive will never get any message.
The erlang docs for the ssl module states that the socket created by calling transport_accept/1 should inherit the options set for the listener socket. The first process inherits the options when it does transport_accept/3, but for some reason the second process doesn’t.
You can inspect the options with
I have no idea why this happens, but a workaround is to explicitly set the options on the newly accepted socket