Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Reception UDP depuis une application C en JAVA

3 réponses
Avatar
Bibu
Bonjour,

A l'heure actuelle, sur ma machine, une application cod=E9e en C envoi une =
structure via l'UDP =E0 une application Java.
J'ai donc commenc=E9 par cr=E9er la classe Java correspondant =E0 ma struct=
ure en C.
Cependant je bloque sur comment d=E9coder les informations re=E7ues, avec J=
NI, je suis arriv=E9 =E0 r=E9cup=E9rer mes donn=E9es en C sur l'application=
Java, mais ma structure =E9tant tr=E8s complexe, cette m=E9thode est a oub=
lier ...

En vous remerciant par avance, parce que l=E0 ... je s=E8che totalement !

3 réponses

Avatar
NotMe
Le 25/04/2012 11:38, Bibu a écrit :
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 structure en C.
Cependant je bloque sur comment décoder les informations reçues, avec JNI, 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 oublier ...

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
Avatar
kanaziwok
Merci NotMe, mais j'aimerais le faire soit en C avec JNI soit en Java mais sans JNA ... bref, je crois que je suis bien partis pour tout faire avec JN I à la main ...
Avatar
Marc Petit-Huguenin
On 04/25/2012 05:00 AM, wrote:
Merci NotMe, mais j'aimerais le faire soit en C avec JNI soit en Java mais sans JNA ... bref, je crois que je suis bien partis pour tout faire avec JNI à la main ...



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