[erlang-questions] "Bad value on output port" when my erlang process communicates with a c-node

Richard Evans <>
Wed Nov 2 11:17:28 CET 2011


I have a problem which I don't understand. Very grateful for any info
you guys have.

My erlang server is communicating with a c-node, using the example
code in Joe Armstrong's book.

So for example, when the server is created:

create() ->
    spawn(fun() ->
        process_flag(trap_exit, true),
        Port = open_port({spawn,
"../ltp_server/Server/build/Debug/server"}, [{packet, 2}]),
        loop(self(), Port)
    end).

Here is the main loop:

loop(Server, Port) ->
    receive
        {call, Caller, Msg} ->
            Port ! {self(), {command, encode(Msg)}},
            receive
                {Port, {data, Data}} ->
                    Caller ! {Server, decode(Data)}
            end,
            loop(Server, Port);
        stop ->
            Port ! {self(), close},
            receive
                {Port, closed} -> exit(normal)
            end;
        {'EXIT', Port, Reason} ->
            exit({port_terminated, Reason})
    end.

The problem I have is that if the Msg I am sending to the c-node has
certain special characters in it, I get "Bad value on output port".

For example, this encoding line below generates the error:

encode({perform_choice, C, Choice, Spin}) ->
[?SERVER_INSTRUCTION_PERFORM_CHOICE, Spin | C] ++ ['#' | Choice]; %
delimiting the two strings with '#'

But if I delimit with '~" instead of '#', the problem goes away:

encode({perform_choice, C, Choice, Spin}) ->
[?SERVER_INSTRUCTION_PERFORM_CHOICE, Spin | C] ++ [~' | Choice]; %
delimiting the two strings with '~'

I *thought* you could send any ascii value 0-255 from erlang to c. So
'#" would be just as legitimate as '~'. Can you guys shed any light on
why I am seeing this problem?

thanks in advance,

Richard



More information about the erlang-questions mailing list