<div dir="ltr"><div>When using {packet, 4} the length does not include the length of the header.<br><br></div>Dan.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jun 27, 2013 at 12:39 AM, H.C. v. Stockhausen <span dir="ltr"><<a href="mailto:hc@vst.io" target="_blank">hc@vst.io</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
this is my first post to this group. Hello everyone.<br>
<br>
I am writing a client that connects to a server via ssl:connect. Upon<br>
connection the server returns a <greeting/>. Thereafter the client<br>
sends commands. The protocol has a 4 byte header that specifies the<br>
payload length followed by the xml payload.<br>
<br>
Here's an extract of my initial attempt that works:<br>
<br>
%%%%%%%%%%%%%%%%%%%%%%%%<br>
...<br>
-behaviour(gen_server).<br>
-define(HEADER_SIZE, 4).<br>
...<br>
<br>
init([]) -><br>
    {ok, Socket} = ssl:connect(<br>
        <host>, <port>, [<br>
        binary,<br>
        {active, false},<br>
        {keepalive, true},<br>
        {certfile, <some/path>},<br>
        {keyfile, <some/path>}<br>
        ]),<br>
    {ok, #state{socket=Socket}, 0}.<br>
<br>
...<br>
<br>
handle_info(timeout, State) -><br>
    {ok, _Greeting} = recv(State#state.socket),<br>
    {noreply, State}.<br>
<br>
...<br>
% internal helpers<br>
<br>
send(Socket, Msg) -><br>
    Length = string:len(Msg) + ?HEADER_SIZE,<br>
    Data = <<Length:32, (list_to_binary(Msg))/binary>>,<br>
    ok = ssl:send(Socket, Data),<br>
    ok.<br>
<br>
recv(Socket) -><br>
    {ok, <<Length:32/integer>>} = ssl:recv(Socket, ?HEADER_SIZE),<br>
    {ok, Data} = ssl:recv(Socket, Length-?HEADER_SIZE),<br>
    Response = binary_to_list(Data),<br>
    {ok, Response}.<br>
<br>
%%%%%%%%%%%%%%%%%%%%%%%%<br>
<br>
I have two questions.<br>
<br>
1) As you can see init()/1 has a 0 timeout to trigger<br>
handle_info(timeout, State) which then fetches and discard the<br>
greeting message.<br>
<br>
Shouldn't I be able to just set {active, once} instead and handle the<br>
initial server response in a matching handle_info clause? I tried but<br>
it's never triggered. All I receive after a while is<br>
{ssl_closed,{sslsocket,new_ssl,<0.65.0>}}.<br>
<br>
2) I manually build and unwrap the header/payload but isn't that what<br>
{packet, 4} is for? I also tried that but the server does not respond,<br>
as incidentally was the case when I initially didn't set the header<br>
properly.<br>
<br>
I realise that there are a lot of list_to_binary_to_list_to...<br>
conversions but that's another issue, isn't it?<br>
<br>
Is this how {active, once} and {packet, 4} should work or am I missing<br>
something, such as differences when it comes to ssl or client vs.<br>
server behaviour?<br>
<br>
Best regards,<br>
Hans Christian<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div>