0

I am trying to transfer data through TCP with Flatbuffers, The server is java but the client is C#, which complicates it heavily.

The schema i am using is:

namespace NetObjects;

union NetParameters { NetLogin, NetRegister }

table NetMessage {
  parameters:NetParameters;
}

table NetLogin{
    id: int;
    email:string;
    password:string;
}

table NetRegister{
    id: int;
    email:string;
    password:string;
}

root_type NetMessage;
file_identifier "MESG";

How i send it c#:

 FlatBufferBuilder fbb = new FlatBufferBuilder(1);

            String buffer = "EMAILCHE";
            StringOffset id = fbb.CreateString(buffer);
            StringOffset idd = fbb.CreateString("PasswordO?");

            NetLogin.StartNetLogin(fbb);
            NetLogin.AddEmail(fbb, id);
            NetLogin.AddPassword(fbb, idd);
            Offset<NetLogin> offset = NetLogin.EndNetLogin(fbb);

            NetMessage.StartNetMessage(fbb);

            NetMessage.AddParametersType(fbb, NetParameters.NetRegister);
            NetMessage.AddParameters(fbb, offset.Value);

            Offset<NetMessage> par = NetMessage.EndNetMessage(fbb);

            NetMessage.FinishNetMessageBuffer(fbb, par);

            _socket.Send(fbb.DataBuffer.Data, 0, fbb.DataBuffer.Data.Length, SocketFlags.None);

How i receive it java:

DataInputStream in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
try (ByteArrayOutputStream os = new ByteArrayOutputStream())
                {
                    byte[] buffer = new byte[0xFFFF];

                    for (int len; (len = in.read(buffer)) != -1;) {
                        os.write(buffer, 0, len);
                        os.flush();
                        Handle(os.toByteArray());
                        System.out.println("Step: " + 4);
                    }
                }

Handle:

public void Handle(byte[] bytes){

        ByteBuffer bff = ByteBuffer.wrap(bytes);

        NetMessage.NetMessageBufferHasIdentifier(bff); // ALWAYS FALSE IS RETURNED

        NetMessage lg = NetMessage.getRootAsNetMessage(bff);

        System.out.println(lg.parametersType()); // 0 is returned
}
4

1 回答 1

0
_socket.Send(fbb.DataBuffer.Data, fbb.Offset, fbb.DataBuffer.Data.Length,  SocketFlags.None);
于 2016-02-21T20:54:39.593 回答