Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web Development
Expertise: Beginner
Mar 19, 1997

Accessing a C Library from Java Applet

I want to be able use a Web frontend for a database application. I would like to use Java to process the user input and C for accessing the database. My question is: Is there any way I can access a dynamically linked library written in C from a Java applet? Both client and server are Unix workstations and I'm using Netscape as my browser.

Once Sun's JDBC specification is finalized and implemented by the major database vendors, then database access from Java applets will be pretty straightforward (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 assume this is for an intranet project, not something for users all over the Internet.

You have written a bunch of C functions that you can put in a library on the server or the client--whatever works. If you want to call these functions directly from Java, you need to turn these C functions into "native" Java functions. The procedure is explained in some detail in the Java tutorial on the java.sun.com web site, as well as in the book Teach yourself 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 is enhanced 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 of Netscape, they'd have to reconfigure your enhancements.

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

You can write a small server program that answers requests to a particular port, calls your C functions and sends the answer back through the socket connection. Your client applet must then open a socket to that port on the server, send the request and read off the answer. You can write that server program in C, if you are familiar with socket programming in C. Or you can write it in Java. All things considered, I'd recommend writing it in C because then you can make those C calls to your own library without having to bother with the native methods. Note that your server program must run on 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 reference for this is Richard Stevens' book on network programming.

If you don't want to deal with that, you can go the CGI route instead. I know, you don't want to touch Perl. But you don't need to use Perl to write a CGI script. You can write a CGI script in any language that can read from standard input and write to standard output. In particular, you can write them in C or Java. If you use HTML forms, then there is a good reason to use Perl--it can parse the funny URL encoding in a couple of lines of Perl magic. But if you design your own forms in Java, you can send the form contents 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 do it by hand. The code below gives a framework that you can modify for your own purpose.

Suppose your Java program needs to know the price of an item. It sends the item name to the CGI script and reads the answer (the price) back. Here is the 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\n";

     out.writeBytes("POST " + scriptname + " HTTP/1.0\r\n"

        + "Content-type: application/octet-stream\r\n"

        + "Content-length: " + query.length() + "\r\n\r\n"

        + query;

     String line;

     String reply = "";

     while ((line = in.readLine()) != null) reply += line + "\n";




On the server side, life is much simpler. The HTTP daemon deals with all the socket stuff. You only need to read the query string from standard input and send the response to standard output. Suppose you write the server 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;


DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date