[erlang-questions] Bi-directional Java/Erlang socket communication?
Dan Rubino
webaccounts@REDACTED
Sat Sep 13 15:59:11 CEST 2008
Hi all,
I have an Erlang process which uses gen_tcp:listen/recv/send.
Now I can send/receive messages Erlang<->Erlang but if I use a Java
process I can only send messages to Erlang from Java and not vice versa.
The Java used to send and receive is pretty simple:
Socket s = new Socket(host, 2345);
System.out.println("Connected to socket on: " + host + ":" + 2345);
DataOutputStream os = new DataOutputStream(s.getOutputStream());
String a = "Hello, world |23|24|26.";
os.write(a.getBytes());
BufferedReader in = new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println("echo: " + in.readLine());
The Erlang code which works (broken down but in reality is across
multiple modules):
{ok, Listen} = gen_tcp:listen(Port, [binary, {packet, 0}, {reuseaddr,
true}, {active, false}]),
Which leads to:
receive
true ->
{ok, Socket} = gen_tcp:accept(Listen),
try gen_tcp:accept(Listen) of
{ok, Socket} ->
spawn(fun() -> parallel_loop(Listen, CallBackMod) end),
CallBackMod:handle_call({socket,Socket}),
which leads to:
handle_call({socket, Socket}) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
io:format("Data received: ~p~n", [Data]),
gen_tcp:send(Socket, term_to_binary("Test String")),
handle_call({socket, Socket});
{error, closed} ->
io:format("Socket closed.~n"),
ok;
Val ->
io:format("Val: ~p~n", [Val])
end.
Like I say, the Erlang process can read the data sent from the Java process too it but for some reason
which I am not totally sure of - the java process is unable to pickup the data being sent back from the
Erlang process using gen_tcp:send()
Has anyone here got any experience performing socket communication between Erlang and Java?
There doesnt seem to be any useful articles on the Net around this unfortunately.
Any help greatly appreciated - a java example would be great!
Thanks
More information about the erlang-questions
mailing list