Accessing a C Library from Java Applet

Accessing a C Library from Java Applet

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.

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 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("", 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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as