[erlang-questions] Bi-directional Java/Erlang socket communication?

Pablo Polvorin <>
Sat Sep 13 17:20:29 CEST 2008


I guess readLine() is blocked waiting to receive a line.., isn't it?
also, gen_tcp:send(Socket,<<"Test String">>) is probably what you want
instead of using term_to_binary/1



2008/9/13 Dan Rubino <>

> 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
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--
pablo
http://ppolv.wordpress.com
----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080913/4d0fd3d3/attachment.html>


More information about the erlang-questions mailing list