如何从 Java 向 Erlang 发送消息?

发布于 2024-10-01 07:19:46 字数 349 浏览 7 评论 0原文

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

这是正确的做法吗?

这样的消息看起来怎么样?

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

http://www.trapexit.org/How_to_communicate_java_and_erlang

I'm making a application in Erlang, with a GUI in Java.
I've managed to establish a connection between the to languages, but now i need to (i guess) send a message from Java to Erlang, every time I e.g press a button.

Is that the right way to go?

How would such a message look?

I've found a few good sites about this form of integration, but I feel like im not getting everything.

http://www.trapexit.org/How_to_communicate_java_and_erlang

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花肆意 2024-10-08 07:19:46

除了通过 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

Besides classic Java-Erlang communication via OTP jinterface you can research such methods like:

 - 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)

You need to learn the shape of your traffic and choose the best solution.
Jinterface is not so bad, tho.. (here is official doc: http://www.erlang.org/doc/apps/jinterface/jinterface_users_guide.html)

地狱即天堂 2024-10-08 07:19:46

如果 jinterface 太复杂,你可以只使用 open_port 上的 packet 选项并使用

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);

To read packet from erlang and to write use:

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

在 erlang 端,这将与

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

If you needlarge packet use {packet, 2} or {packet, 4并适应 java.
在数据包内部,您可以在双方上运行您喜欢的任何协议。

If jinterface is too complicated you might just use the packet option on open_port and use

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);

To read packets from erlang and to write use:

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

On the erlang side this would match with

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

If you need larger packets use {packet, 2} or {packet, 4} and adapt the java.
Inside the packets you can run whatever protocol you like on both sides.

長街聽風 2024-10-08 07:19:46

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

I am working on an application similar to yours: C++ GUI and Erlang server. I use TCP sockets to exchange messages between the GUI and server, and Erlang server patterns for handling requests (I may have more than one GUI hooked up to the server at the same time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文