Delphi应用程序和android应用程序之间使用Union进行通信

发布于 2024-12-09 04:07:06 字数 1902 浏览 1 评论 0原文

我创建了一个 Android 应用程序来与另一个 Delphi 应用程序进行通信。 delphi应用程序接受的数据是并集。我使用 UDP 向其发送我定义的创建类的类型。但是delphi应用程序接收到的数据被错误地解释。所以我想知道java中是否存在实现联合类型的方法。 我已经看过这个 http://lambda-the-ultimate.org/node/2694 ,但我不明白他在那堂课上做了什么。

编辑:抱歉,但我只有我的代码,因为delphi应用程序是由另一个程序员创建的。不过我已经解决了这个问题。 delphi应用程序希望字节按小端排序,而我以大端发送它们,所以在以这种方式用流序列化数据后:

ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try{
    dos.writeLong(Double.doubleToRawLongBits(x));
    dos.flush();
    px=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(y));
    dos.flush();
    py=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(z));
    dos.flush();
    pz=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(a));
    dos.flush();
    ga=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(b));
    dos.flush();
    gb=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(c));
    dos.flush();
    gc=baos.toByteArray();
    baos.reset();
}catch(Exception e){}

然后我用一些cicles颠倒了顺序,如下所示:

ByteBuffer  bb = ByteBuffer.allocate(48);
//bb.order(ByteOrder.LITTLE_ENDIAN);
for(int i=7;i>=0;i--)
  bb.put(messaggio.getPx()[i]);
for(int i=7;i>=0;i--)
  bb.put(messaggio.getPy()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getPz()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getGa()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getGb()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getGc()[i]);
byte[] messbyte=bb.array();

你看到messaggio.getPx等等因为最初的想法是发送一个我的类的对象,但我通过wireshark看到序列化对象会带来一些信息,例如包的名称。所以我决定发送一个 48 字节的字节向量(是六个双字段)。我通过 getPx(),getPy(),...., 方法选择这个字段。 但服务器还存在其他问题。但对于这些问题我会和delphi程序员讨论。

I have created an android application for communicate with another Delphi application . The data that delphi application accepts is union. I send with UDP to it a type that I defined creating a my class. But the data that delphi application receive is incorrectly interpretated. So I want know if exist a method for implement a union type in java.
I already look this http://lambda-the-ultimate.org/node/2694, but I don't understand what he do with that class.

EDIT: Sorry, but I have only my code, because the delphi application was created by another programmer. However I have resolved the problem. The delphi application wanted byte ordered by little endian, while I sent they in big endian, so after serialize the data with the stream in this way:

ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try{
    dos.writeLong(Double.doubleToRawLongBits(x));
    dos.flush();
    px=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(y));
    dos.flush();
    py=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(z));
    dos.flush();
    pz=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(a));
    dos.flush();
    ga=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(b));
    dos.flush();
    gb=baos.toByteArray();
    baos.reset();
    dos.writeLong(Double.doubleToRawLongBits(c));
    dos.flush();
    gc=baos.toByteArray();
    baos.reset();
}catch(Exception e){}

then i have inverted the order with some for cicles as these:

ByteBuffer  bb = ByteBuffer.allocate(48);
//bb.order(ByteOrder.LITTLE_ENDIAN);
for(int i=7;i>=0;i--)
  bb.put(messaggio.getPx()[i]);
for(int i=7;i>=0;i--)
  bb.put(messaggio.getPy()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getPz()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getGa()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getGb()[i]);
for(int i=7;i>=0;i--)
 bb.put(messaggio.getGc()[i]);
byte[] messbyte=bb.array();

You see messaggio.getPx and so on because the initial idea was of sent an object of a my class, but I see through wireshark that the serialize object lead with him some information like the name of the package. So I decided to sent a byte vector of 48 bytes (was six double field). I pick this field cross through the getPx(),getPy(),...., method.
But there are other problem in the server yet. But for these problems I will talk with the delphi programmer.

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

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

发布评论

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

评论(1

落墨 2024-12-16 04:07:06

Java不支持在同一存储位置存储两种不同类型的值。 Lambda the Ultimate 论坛上给出的 Either 类不是 C 和 Pascal 具有联合类型的联合。 C 和 Pascal 允许您将值存储在一个字段中并从另一个字段中读取值,然后您将获得隐式类型转换。论坛上显示的 Java 类允许您为保存任一类型值的联合创建一个值,但是一旦您创建了 Either 值,您就只能读取您存储的值;您无法读取其他类型的内容。

要在运行时从套接字上的数据创建这样的值,您需要知道该字段的类型。当您从套接字读取值时,确定它的类型并创建正确类型的 Either 子类(Either.LeftEither.Right< /代码>)。序列化 Java 数据时,调用 leftright 来获取当前值。

Java does not support storing values of two different types in the same storage location. The Either class given at the Lambda the Ultimate forum is not a union in the way that C and Pascal have union types. C and Pascal let you store a value in one field and read from another field, and you get an implicit type cast. The Java class shown at the forum lets you create a value for the union that holds a value of either type, but once you've created the Either value, you can only read the value you stored; you cannot read from the other type.

To create such a value at run time from the data on the socket, you'd need to know the type of the field. As you read the value off the socket, determine which type it is and create the Either subclass of the proper type (either Either.Left or Either.Right). When serializing your Java data, call either left or right to get the current value.

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