Non-blocking TCP server on trapexit.orgNon-blocking TCP server on trapexit.org解釋瞭如何構建基於tcp_gen的服務器,但我想修改此示例並使其適用於ssl。現在我完全不知道如何替換如何使用ssl加密構建非阻塞TCP服務器
{ok, Ref} = prim_inet:async_accept(Listen_socket, -1)
Non-blocking TCP server on trapexit.orgNon-blocking TCP server on trapexit.org解釋瞭如何構建基於tcp_gen的服務器,但我想修改此示例並使其適用於ssl。現在我完全不知道如何替換如何使用ssl加密構建非阻塞TCP服務器
{ok, Ref} = prim_inet:async_accept(Listen_socket, -1)
多線程
-module(echo_ssl).
-compile([export_all]).
main() ->
application:start(crypto),
application:start(ssl),
ssl:seed("TODO random here"),
{ok, ListenSocket} = ssl:listen(2840, [
{ssl_imp, new},
{active, false},
{verify, 0},
{mode,binary},
{cacertfile, "certs/etc/server/cacerts.pem"},
{certfile, "certs/etc/server/cert.pem"},
{keyfile, "certs/etc/server/key.pem"}
]),
io:format("ready to accept connections at port 2840 ~p\n", [ListenSocket]),
server_loop(ListenSocket).
server_loop(ListenSocket) ->
{ok, Socket} = ssl:transport_accept(ListenSocket),
io:format("accepted connection from ~p\n", [ssl:peername(Socket)]),
ssl:ssl_accept(Socket),
spawn(fun() -> loop(Socket) end),
server_loop(ListenSocket).
loop(Socket) ->
io:format("waiting for packet~n"),
case ssl:recv(Socket, 1000, 2000) of
{ok, Data} ->
io:format("received data: ~s~n", [binary_to_list(Data)]),
Return = ssl:send(Socket, Data),
io:format("sending ~p~n", [Return]),
loop(Socket);
{error, timeout} ->
loop(Socket);
Else ->
io:format("crap ~p~n",[Else])
end.
簡單echo_ssl豈不SSL是在堆棧高於此的層?運輸仍然是TCP。 – spender 2009-09-21 11:14:51