<div dir="ltr">I guess readLine() is blocked waiting to receive a line.., isn't it?<br>also, gen_tcp:send(Socket,<<"Test String">>) is probably what you want instead of using term_to_binary/1<br><br>
<br><br><div class="gmail_quote">2008/9/13 Dan Rubino <span dir="ltr"><<a href="mailto:webaccounts@rubino.co.uk">webaccounts@rubino.co.uk</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I have an Erlang process which uses gen_tcp:listen/recv/send.<br>
<br>
Now I can send/receive messages Erlang<->Erlang but if I use a Java<br>
process I can only send messages to Erlang from Java and not vice versa.<br>
<br>
The Java used to send and receive is pretty simple:<br>
<br>
Socket s = new Socket(host, 2345);<br>
<br>
System.out.println("Connected to socket on: " + host + ":" + 2345);<br>
<br>
DataOutputStream os = new DataOutputStream(s.getOutputStream());<br>
<br>
String a = "Hello, world |23|24|26.";<br>
<br>
os.write(a.getBytes());<br>
<br>
BufferedReader in = new BufferedReader(new<br>
InputStreamReader(s.getInputStream()));<br>
<br>
System.out.println("echo: " + in.readLine());<br>
<br>
<br>
The Erlang code which works (broken down but in reality is across<br>
multiple modules):<br>
<br>
{ok, Listen} = gen_tcp:listen(Port, [binary, {packet, 0}, {reuseaddr,<br>
true}, {active, false}]),<br>
<br>
Which leads to:<br>
<br>
receive<br>
true -><br>
{ok, Socket} = gen_tcp:accept(Listen),<br>
try gen_tcp:accept(Listen) of<br>
{ok, Socket} -><br>
spawn(fun() -> parallel_loop(Listen, CallBackMod) end),<br>
CallBackMod:handle_call({socket,Socket}),<br>
<br>
which leads to:<br>
<br>
handle_call({socket, Socket}) -><br>
case gen_tcp:recv(Socket, 0) of<br>
{ok, Data} -><br>
io:format("Data received: ~p~n", [Data]),<br>
gen_tcp:send(Socket, term_to_binary("Test String")),<br>
handle_call({socket, Socket});<br>
{error, closed} -><br>
io:format("Socket closed.~n"),<br>
ok;<br>
Val -><br>
io:format("Val: ~p~n", [Val])<br>
end.<br>
<br>
Like I say, the Erlang process can read the data sent from the Java process too it but for some reason<br>
which I am not totally sure of - the java process is unable to pickup the data being sent back from the<br>
Erlang process using gen_tcp:send()<br>
<br>
Has anyone here got any experience performing socket communication between Erlang and Java?<br>
<br>
There doesnt seem to be any useful articles on the Net around this unfortunately.<br>
<br>
Any help greatly appreciated - a java example would be great!<br>
<br>
Thanks<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>--<br>pablo <br><a href="http://ppolv.wordpress.com">http://ppolv.wordpress.com</a><br>----<br>
</div>