Accessing a C Library from Java Applet

Question:
I want to be able use a Web frontend for a databaseapplication. I would like to use Java to process theuser input and C for accessing the database. Myquestion is: Is there any way I can access a dynamically linked library written in C from a Java applet? Bothclient and server are Unix workstations and I’m usingNetscape as my browser.

Answer:
Once Sun’s JDBC specification is finalized and implemented by the majordatabase vendors, then database access from Java applets will be prettystraightforward (at least if you know SQL).But that doesn’t help you if you need a solution now.

Since you know that all clients will be running the same hardware, I assumethis is for an intranet project, not something for users all over theInternet.

You have written a bunch of C functions that you can put in a library onthe server or the client–whatever works. If you want to call thesefunctions directly from Java, you need to turn these C functions into”native” Java functions. The procedure is explained in some detail in theJava tutorial on the java.sun.com web site, as well as in the book Teachyourself Java in 21 days, by Laura Lemay and Charles Perkins (SAMS, 1995).

Since all your users are using a single platform, you could, in princple,give each potential client a version of the Netscape Java library that isenhanced by your native methods, together with your dynamic link library.But that sounds like a lot of trouble. Each time someone gets a new copy ofNetscape, they’d have to reconfigure your enhancements.

It seems easier to put the C code on the server. Then you need to establisha communication path between the clients and the server. There are twopossibilities. Either you implement a small database server program, or youuse CGI.

You can write a small server program that answers requests to a particularport, calls your C functions and sends the answer back through the socketconnection. Your client applet must then open a socket to that port on theserver, send the request and read off the answer. You can write that serverprogram in C, if you are familiar with socket programming in C. Or you canwrite it in Java. All things considered, I’d recommend writing it in Cbecause then you can make those C calls to your own library without havingto bother with the native methods. Note that your server program must runon the exact same server that contains the applet code.

Writing a server in C takes a bit of skill–you need to listen to a port,fork a new process, receive and send data through sockets. A good referencefor this is Richard Stevens’ book on network programming.

If you don’t want to deal with that, you can go the CGI route instead. Iknow, you don’t want to touch Perl. But you don’t need to use Perl to writea CGI script. You can write a CGI script in any language that can read fromstandard input and write to standard output. In particular, you can writethem in C or Java. If you use HTML forms, then there is a good reason touse Perl–it can parse the funny URL encoding in a couple of lines of Perlmagic. But if you design your own forms in Java, you can send the formcontents to the CGI script in any format you like.

There is no Java library method to make a CGI call. But it isn’t hard to doit by hand. The code below gives a framework that you can modify for yourown purpose.

Suppose your Java program needs to know the price of an item. It sends theitem name to the CGI script and reads the answer (the price) back. Here isthe code on the Java side (ignoring those pesky exceptions).

     Socket s - new Socket("yourserver.com", 80);     DataOutputStream out = new DataOutputStream(s.getOutputStream());     DataInputStream in = new DataInputStream(s.getInputStream());     string scriptname = "/cgi-bin/pricequery";     string query = "PRICE Toaster oven
";     out.writeBytes("POST " + scriptname + " HTTP/1.0
"        + "Content-type: application/octet-stream
"        + "Content-length: " + query.length() + "

"        + query;     String line;     String reply = "";     while ((line = in.readLine()) != null) reply += line + "
";     in.close();     out.close();     

On the server side, life is much simpler. The HTTP daemon deals with allthe socket stuff. You only need to read the query string from standardinput and send the response to standard output. Suppose you write theserver program in C:

     int main()     {  char query[200];        fgets(query, sizeof(query), stdin);        /* call C functions to get the response */        printf(...); /* print response */        return EXIT_SUCCESS;     }     
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: