open_port function problem.

Joachim Durchholz joachim.durchholz@REDACTED
Fri Dec 19 09:50:40 CET 2003

Chenniappan,Dhanasekaran [DBA] wrote:
> hai, 
>   we are working on erlang port communication program which communicate with
> c .exe . for that we use open_port like
> "open_port({spawn, ExtPrg}, [{packet, 2}]),"
> our problem is when we are sending number 26 through that port the port get
> closed.what may be possibly wrong with this. and when we give 10 through
> buffer it always prints we assume that it may be due to ascii property of 10
> & 13 which is new line and cairrage return respectively, but how can these
> things be handled effectively,there is no clue regarding the port close when
> sending 26. Evenn the example foo also reflect the same resullt. Please give
> us an solution in this problem.

You're probably working on a Windows system, and the port got opened in 
text mode.
Under Windows, text mode file handles will interpret code 10 (newline) 
as line end (and store the bytes 13 and 10, carriage return and newline, 
to establish conformance with Windows conventions). Code 26 is 
Control-Z, which means end-of-file (this is a really ancient convention 
but kept for compatibility).

To get rid of these effects, open the port in "binary" mode; in this 
mode, no translation takes place.

Text mode interpretation may happen both on the sending and receiving 
end of the pipe. IOW you should not only check the Erlang side of 
things, but also the C side.
Actually, when I think of it, it's most likely that you're just using 
the stdin handle in the C program, which is always opened in text mode 
for you; you have to change its mode (not sure how to do this, you're 
probably in for an extensive search in MSDN).

Keep us posted on results, this might be an interesting information 
snippet to be added to the Erlang documentation :-)


More information about the erlang-questions mailing list