[erlang-questions] erlang port and C executable

Robert Raschke rtrlists@REDACTED
Sat Nov 22 13:26:02 CET 2008


Hi Joe,

On Fri, Nov 21, 2008 at 11:37 PM, Joe Williams <joe@REDACTED> wrote:
> Hello,
>
> I am having an issue when I attempt to send and receive messages to a C
> executable. I believe the task should be fairly trivial but I am getting
> hung up on sending messages to the port. Here's what I have so far:
>
>> Eshell V5.6.3  (abort with ^G)
>> 1> Cmd = "SOME_EXECUTABLE",
>> 1> Port = open_port({spawn, Cmd}, []).
>> #Port<0.93>
>> 2> receive
>> 2> {Port, {data, Data}} ->
>> 2> io:format("~p~n", [Data]);
>> 2> Error ->
>> 2> io:format("~p~n", [Error])
>> 2> end.
>> "TEXT FROM THE EXECUTABLE"
>> ok
>> 3> Port ! <<"TEXT TO THE EXECUTABLE">>.
>> ** exception exit: badsig
>> 4> Port ! <<"TEXT TO THE EXECUTABLE">>.
>> <<"TEXT TO THE EXECUTABLE">>
>> 5> receive
>> 5> {Port, {data, Data}} ->
>> 5> io:format("~p~n", [Data]);
>> 5> Error ->
>> 5> io:format("~p~n", [Error])
>> 5> end.
>
> After the last end my code just hangs and waits to receive a message
> rather than receiving the response from "TEXT TO THE EXECUTABLE". Since
> it received the first message properly I assume it would receive further
> responses in the same way. So I have two questions, the first is what
> does "exception exit: badsig" generally mean? The second how can I write
> this in such a way that I can send and receive strings until I have the
> program exit?
>
> Any guidance is appreciated.
>

I would suggest not using the shell for this, but to write a small
module. For example, your second receive uses already bound names
(e.g. Data) so that won't do what you expect.

Have a look at the Documentation: Erlang Programming ->
Interoperability Tutorial, Section 4 Ports
(http://www.erlang.org/doc/tutorial/part_frame.html) where you can see
a minimal port program wrapped by an Erlang process.

Robby



More information about the erlang-questions mailing list