开发者

Communication between Delphi application and android application using Union

I have created an android application for communicate with another Delphi application . The data that delphi application accepts is union. I sen开发者_StackOverflow社区d 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜