Reception UDP depuis une application C en JAVA
Le
Bibu

Bonjour,
A l'heure actuelle, sur ma machine, une application codée en C envoi une =
structure via l'UDP à une application Java.
J'ai donc commencé par créer la classe Java correspondant à ma struct=
ure en C.
Cependant je bloque sur comment décoder les informations reçues, avec J=
NI, je suis arrivé à récupérer mes données en C sur l'application=
Java, mais ma structure étant très complexe, cette méthode est a oub=
lier
En vous remerciant par avance, parce que là je sèche totalement !
A l'heure actuelle, sur ma machine, une application codée en C envoi une =
structure via l'UDP à une application Java.
J'ai donc commencé par créer la classe Java correspondant à ma struct=
ure en C.
Cependant je bloque sur comment décoder les informations reçues, avec J=
NI, je suis arrivé à récupérer mes données en C sur l'application=
Java, mais ma structure étant très complexe, cette méthode est a oub=
lier
En vous remerciant par avance, parce que là je sèche totalement !
http://stackoverflow.com/questions/3923299/how-to-pass-c-structs-back-and-forth-to-java-code-in-jni
Ca n'est pas tres complique a faire en pure Java, en utilisant java.nio.* et
java.nio.channels.*
Les classes dans java.nio.channels sont utilises pour recevoir les donnes dans
un objet de type ByteBuffer. Ensuite c'est simple de parser le contenu d'un
ByteBuffer. Voila un exemple partiel pour RELOAD[1] (code under AGPL3):
void parse(ByteBuffer buffer)
throws IOException {
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long reloToken = buffer.getInt() & 0xFFFFFFFFl;
if (reloToken != 0xd2454c4fl) {
throw new IOException("relo_token invalid");
}
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
byte[] overlay = new byte[4];
buffer.get(overlay);
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int sequence = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < 1) {
throw new IOException("Buffer too small");
}
int version = buffer.get() & 0xFF;
if (buffer.remaining() < 1) {
throw new IOException("Buffer too small");
}
short ttl = (short)(buffer.get() & 0xFF);
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long fragment = buffer.getInt() & 0xFFFFFFFFl;
boolean last = (fragment & 0x40000000) == 0x40000000;
int offset = (int)(fragment & 0xFFFFFF);
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long length = buffer.getInt() & 0xFFFFFFFFl;
if (buffer.remaining() < length - 20) {
throw new IOException("Buffer too small");
}
if (buffer.remaining() < 8) {
throw new IOException("Buffer too small");
}
long transactionId = buffer.getLong();
if (buffer.remaining() < 4) {
throw new IOException("Buffer too small");
}
long maxResponseLength = buffer.getInt() & 0xFFFFFFFFl;
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int viaListLength = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int destinationListLength = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < 2) {
throw new IOException("Buffer too small");
}
int optionsLength = buffer.getShort() & 0xFFFF;
if (buffer.remaining() < viaListLength) {
throw new IOException("Buffer too small");
}
int limit = buffer.limit();
buffer.limit(buffer.position() + viaListLength);
List<Address> vias = viaListLength > 0 ? Address.getAllByName(buffer) : new
ArrayList<Address>();
buffer.limit(limit);
if (buffer.remaining() < destinationListLength) {
throw new IOException("Buffer too small");
}
limit = buffer.limit();
buffer.limit(buffer.position() + destinationListLength);
List<Address> destinations = destinationListLength > 0 ?
Address.getAllByName(buffer) : new ArrayList<Address>();
buffer.limit(limit);
if (buffer.remaining() < optionsLength) {
throw new IOException("Buffer too small");
}
buffer.position(buffer.position() + optionsLength);
}
[1] https://www.ietf.org/id/draft-ietf-p2psip-base-21.txt