Solved

Java and C struct

Posted on 2001-06-29
6
561 Views
Last Modified: 2008-03-17
Hi,
I have a very basic question

what's the best way to read "C struct" in java

ex: I have a java client.
The client eastablishes a socket connection
to a server program (written in c)

now the client has to read the "c structures"
send by the server.

How do I deal with this.


0
Comment
Question by:sanjay_thakur
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:jasbro
Comment Utility
There are two ways I can think of immediately, one isn't platform independent, the other usually makes people run away screaming...

1.) You need to know the structure (byte by byte) of the the struct.  You will read byte by byte on the Java side, reorder bytes if necessary (depending on platform) and reconstruct the individual data items.  As I said, you will need to know the exact size in bytes of every member, and must have a decent understanding of the machine representation of that data type.

2.) Aaaaaaarrrrrrggggghhhh!!!!  You guessed it, JNI.  This can be done in two different ways...an add on to your server (implemented in Java with a bit of JNI code in between that maps the c struct to a java class) that then serializes the java class and sends it down your socket connection.  The other is just the just moves the c code to the client side...this has the same drawbacks as solution 1 if your client and server are different platforms.  The best is JNI on the server side.


Jason
0
 
LVL 4

Author Comment

by:sanjay_thakur
Comment Utility
I need some more details
Is there any general way so that

I can pack all the information in something similiar
to a structure and send it across to the server?

ex: if I have a structure
struct test
{
        char     *pw_passwd;          
     int     pw_uid;          
     int     pw_gid;          
     char     *pw_comment;    

}

How do I read all the values at once
and write all the data in the structure at once

I dont want to do it with bytes because
the structures I have are really hugh and
It will require a lot of parsing and rearranging.

basically can I make a Packet of some sort
so that I can send all the data at once
rather than in pieces

How complex will be JNI if I want to do this.


0
 
LVL 2

Expert Comment

by:jasbro
Comment Utility
The JNI is not very complicated, but if you've never done JNI before, it can be a bit confusing.  The code for moving the struct in your comment would be similar to:

jclass cls;
jmethodID mid;
jstring str1,str2;

/*MyStruct is the Java class that looks similar to your struct, env is the JVM environment */
cls = (*env)->FindClass(env, "MyStruct");

/* "<init>" tells it to get me the method ID for the constructor with the given signature.
The signature (last arg) says the constructor takes two strings and two integers as parameters, and returns void...even though a constructor doesn't really return a value */
mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;Ljava/lang/String;II)V");

str1 = (*env)->NewStringUTF(env,s.pw_passwd);
str2 = (*env)->NewStringUTF(env,s.pw_comment);

/* Now that we have the method ID, we can call the constructor */
jobj = (*env)->NewObject(env, cls, mid, str1, str2, s.pw_uid, s.pw_gid);

/* if you implement the connection/send of the object in the Java class, you can just call that method, or you can implement the code to send it here, but you have to do a lot more work.  Make sure you make your Java class serializable, it will be easier to send. */  
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Accepted Solution

by:
jasbro earned 100 total points
Comment Utility
If you do want to do it without using JNI, the only thing you can do is byte by byte...and you will have to make the first byte an identifier that tells you what platform you are coming from so that you can correctly order the bytes to recreate the integers and doubles and such..
0
 
LVL 4

Author Comment

by:sanjay_thakur
Comment Utility
Hi I had a simple question

I am very new to JNI.
so bear with me

I have defined two java classes
passwd.class
passwdmain.class

In passwd class I have a native function
public native void commit();

I generate passwd.h
I have passwd.c where I implement commit() method
I generate a shared library Passwdlib.so


In passwdMain class I load the lbrary
System.loadLibrary("passwdLib")
create an Instance of Passwd class
like
passwd obj = new passwd();

call the native function

obj.commit();

but this gives me a "unsatified link error"

If I put everything in one class then it works fine

What do I need to do to make this work?
Is the library loaded in one class available to the other
class.


p.s :I tried loading the library in the passwd class also
but doesn't work

any suggestions?


 









0
 
LVL 2

Expert Comment

by:jasbro
Comment Utility
First, is the exception coming on the loadLibrary call, or the call to your native method?  If it is coming on the load, then it can't find your library...if it is coming on the native method call, make sure that your native method exists in your library (if you used a c++ compiler, it may have mangled the name, also, the function name for the native function will probably be something like "Java_commit_1J".  Did you use javah to generate the header file, and then use the function declaration from that file?


p.s. sorry it took me so long to get to this, I have been on vacation...

Jason
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
sumDigits  challenge 7 60
powerN  challenge 3 46
java  and programming certification ? 4 63
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now