7

我正在用 Java 中的 GUI 用 Erlang 制作一个应用程序。我已经设法在 to 语言之间建立了连接,但现在我需要(我猜)每次按下按钮时从 Java 向 Erlang 发送一条消息。

这是正确的方法吗?

这样的消息会是什么样子?

我找到了一些关于这种集成形式的好网站,但我觉得我没有得到一切。

http://www.trapexit.org/How_to_communicate_java_and_erlang

4

3 回答 3

4

除了通过 OTP jinterface 进行经典的 Java-Erlang 通信外,您还可以研究以下方法:

 - thrift
 - ice from zeroC (no official erlang binding)
 - maybe two http servers on both sides (I like this approach) 
 - protocol buffers (rather not, it is better for larger data transfers)

您需要了解流量的形状并选择最佳解决方案。Jinterface 还不错,虽然..(这里是官方文档:http ://www.erlang.org/doc/apps/jinterface/jinterface_users_guide.html )

于 2010-11-09T13:08:06.130 回答
3

如果 jinterface 太复杂,您可能只使用 open_port 上的数据包选项并使用

byte[] in_buf = new byte[256];
byte[] out_buf = new byte[256];
int in_count = System.in.read ();
int offset = 0; 
do
    {
        int c = System.in.read (in_buf, offset, in_count-offset);
        offset += c;
    }
while (offset < in_count);

要从 erlang 读取数据包并写入使用:

System.out.write(out_count);
System.out.write(out_buf, 0, out_count);

在二郎方面,这将与

open_port({spawn, "<path-to-java> -cp <classpath> your-java-prog", 
          [{packet, 1}]).

如果您需要更大的数据包,请使用 {packet, 2} 或 {packet, 4} 并调整 java.util. 在数据包内,您可以在两侧运行您喜欢的任何协议。

于 2010-11-09T13:32:19.177 回答
1

我正在开发与您类似的应用程序:C++ GUI 和 Erlang 服务器。我使用 TCP 套接字在 GUI 和服务器之间交换消息,并使用 Erlang 服务器模式来处理请求(我可能同时有多个 GUI 连接到服务器)。

于 2010-11-09T14:18:25.657 回答