私は非常に単純なC++コードを作成しました。
#include <iostream>
using namespace std;
int main() {
int message;
cin >> message;
cout << message;
return 0;
}
シェルでテストしました。シェルに入力された値を返します。
しかし、Erlangから呼び出そうとすると、{exit_status、0}が返されます。これは、私が理解しているように、終了したばかりであることを意味します。
Erlangコードはここにあります:
p(Param) ->
?DBG("Starting~n", []),
Cmd = "./Echo\n",
Port = open_port({spawn,Cmd}, [binary,{packet, 4}, exit_status]),
Payload = term_to_binary(list_to_binary(integer_to_list(Param))),
?DBG("Opened the port: ~w~n", [Port]),
erlang:port_command(Port, Payload),
?DBG("Sent command to port: ~w~n", [Payload]),
?DBG("Ready to receive results for command: ~w~n", [Payload]),
receive
{Port, {data, Data}} ->
?DBG("Received data: ~w~n", [Data]),
{result, Text} = binary_to_term(Data),
Blah = binary_to_list(Text),
io:format("~p~n", [Blah]);
Other ->
io:format("Unexpected data: ~p~n", [Other])
end.
Erlangポートをこの単純なC++コードに接続するにはどうすればよいですか?
UPD:
C ++コードを次のように変更しました(役に立たないバージョン):
#include <iostream>
using namespace std;
int main() {
int message;
cin.read(reinterpret_cast<char *>(&message), 4);
cout.write(reinterpret_cast<char *>(&message), 4);
return 0;
}
Erlangポートプロセスは引き続き{exit_value、0}を取得します。このコードをシェルで実行すると、Returnキーを2回押すまで、キーボード入力はエコーされません。