[erlang-questions] Questions on gen_tcp & flexible parsing

Bo <>
Tue Apr 1 17:20:47 CEST 2008

I have now been programming in Erlang for quite a while and most things are really easy to do, but I found some rather tough issues, here the come:

1)  When using the example server from the man pages of the gen_tcp / inet unit I have tried to create an auto update routine, which does a sort of ?MODULE:loop(<some parameters>), but it seems that the sockets just won't let go, stick to the running module and as expected under the circunstances, after the second update Erlang moves the sockets out of the VM and ends the processes, so I cannot create a socket server which permits to be updated in runtime.   
Just for info, under any other cicumstances I have managed to create all my other modules to be updatable and it works just fine, except this simple socket server ??   Does gen_tcp have problems to do the typical ?MODULE:loop(X) ??   

2)  Erlang describes that one can limit the size of the packet to be received by a socket, if it's longer it gets marked invalid and I would suppose that it gets dropped, the parameter is called packet_size.   I would like to limit the packets to be received to be of X size, the example below shows 255, yet I can successfully send large packets to the socket, what's wrong ??   Note: Why do I want this ??   I want to avoid problems where hackers do a buffer overrun hack and crash the server by sending many large packets which then write to memory at some point or crash the server because it exhausts memory, something very common in Windows hacks.  inet states that much depends on the underlying OS, which is Windows in this case, but I do not know if this parameter depends on the OS.

case gen_tcp:listen(LPort,[{active, false},{packet_size, 255}, {packet,0},{recbuf, 255},{sndbuf, 255}]) of
        {ok, ListenSock} ->
I have even used recbuf and sndbuf which do not seem to do anything either.

3) Also, I am writing a small scripted client to test the server part, now, when I am bombarding the server with messages and ask it to send these to the other processes I face the following problem:   first of all and thankfully, Erlang keeps up, the server does not seem to crash, yet, when a client finally crashes with a stack overflow (problably a memory leak or too much data), this particular acceptor on the server side does not exit, although I have included code to make sure that the exit gets trapped, yet this process stays alive, instead of closing.   My question here is has anyone faced this before ??   When a client exits normally no problem ocurrs, only when it crashes.

4) I am building a command parser which needs to parse in commands of the structure {CMD:DETAIL}.   Each time the server receives a packet it may receive several commands of this structure, which is why I need to parse a list of commands and the parsing code could look like this:

parsecmd(T) ->
    parseusr(T, []).

parscmd("{USR:" ++ T, L) ->
    {Url, T1} = collect_cmd(T, reverse("")),
    parsecmd(T1, [Url|L]);

parsecmd([_|T], L) ->
    parsecmd(T, L);

parseusr([], L) ->

collect_cmd("}" ++ T, L) -> {reverse(L, ""), T};
collect_cmd([H|T], L)       -> collect_cmd(T, [H|L]);
collect_cmd([], _)          -> {[],[]}.

I would like to call parsecmd(<CMD>,<LIST TO PARSE>)
The problem is that I cannot pass "{CMD:" as a parameter, like in the above "{USR:", if I do Erlang complains that it cannot process that structure, where "{CMD:" is a list.   Is there any way this can be parametrized ??   Or is there another function ??   And no, I have not used YECC, because I find its working incomprehensible.   I can already parse several commands when I search separately for "{", "}" and ":" but not for "{CMD:" and "}", where CMD is a variable that I want to pass to the the parsing function.

Any help is appreciated, I have researched different unit'mans, the whole Erlang Bible, other examples but I cannot find a solution to those 4 issues, so I am turning to the ever so wise community.  

Thanks in advance,


You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080401/63616321/attachment.html>

More information about the erlang-questions mailing list