Jeeves - Sun's Java Server

Applets/Servlets - Native Functions


The Files to be included in the Project will be
To learn the basics of native functions refer to our native tutorial on the Java Page
Program 1

I Java Applet - Simple Program

aaa.java
class aaa

{      public static void main(String s[])

       {      bbb a;

              System.out.println("Start");

              a = new bbb();

              System.out.println("Before xx");

               a.xx();

               System.out.println("After xx");

         }

}

	

class bbb

{      public native void xx();

       public bbb()

       {

            System.out.println("in bbb");

            System.loadLibrary("sss");

        }

}



sss.c
#include <windows.h>
void *Java_bbb_xx_stub(void * p)

{       MessageBox(0,"Hi","Hi",0);

        return p;

}



sss.def
LIBRARY     sss

EXPORTS

        Java_bbb_xx_stub       



I Servlet Simple Program

s.java
import java.io.*;

import java.servlet.*;



public class s extends Servlet

{



public void service(ServletRequest req,ServletResponse res) throws IOException

{

        bbb a;

        res.setContentType("text/html");

        res.writeHeaders();

        PrintStream out =new PrintStream(res.getOutputStream());

        out.println("Start");

        a=new bbb();

        out.println("Before xx");

        a.xx();

        out.println("After xx");

}

}

class bbb

{      public native void xx();

       public bbb()

       {

            System.loadLibrary("sss");

       }

}

sss.c
#include <windows.h>
void *Java_bbb_xx_stub(void * p)

{       MessageBox(0,"Hi","Hi",0);

        return p;

}

sss.def
LIBRARY     sss

EXPORTS

        Java_bbb_xx_stub       




Program 2

II Java Applet - Ping Program

aaa.java
class aaa

{      public static void main(String s[])

       {      bbb a;

              System.out.println("Start");



              a = new bbb();

              System.out.println("Before xx");

		

               a.xx();

               System.out.println("After xx");

         }

}

	

class bbb

{      public native void xx();

       public bbb()

       {

            System.out.println("in bbb");

            System.loadLibrary("sss");

        }

}

sss.def
LIBRARY     sss

EXPORTS

        Java_bbb_xx_stub       

sss.c
#include <windows.h>


struct o

{

        unsigned char Ttl,Tos,Flags,OptionsSize,*OptionsData;

};

struct

{

        DWORD Address;

        unsigned long  Status,RoundTripTime;

        unsigned short DataSize,Reserved;

        void *Data;                  

        struct o  Options;

} E;



HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent *phostent;

DWORD d;char aa[100];struct o I;



HANDLE ( WINAPI *pIcmpCreateFile )( VOID );

BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );

DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,LPVOID,

                                        LPVOID,DWORD,DWORD);





void *Java_bbb_xx_stub(void * p)

{  

    hIcmp = LoadLibrary( "ICMP.DLL" );

        WSAStartup( 0x0101, &wsa );

		MessageBox(0,"In Java_bb_xx_stub","hi",0);

        phostent = gethostbyname( "www.microsoft.com");

        dwIPAddr = (DWORD *)( *phostent->h_addr_list );

        

        pIcmpCreateFile=GetProcAddress( hIcmp,"IcmpCreateFile");

        pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");

        pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );



        hIP = pIcmpCreateFile();

        I.Ttl=6;

        pIcmpSendEcho(hIP,*dwIPAddr,0,0,&I,&E,sizeof(E),8000 );

        d=E.Address;

        phostent = gethostbyaddr((char *)&d,4,PF_INET);

        sprintf(aa,"gethostbyaddr %p",phostent );

        MessageBox(0,aa,aa,0);

        if ( phostent  != 0 )

                MessageBox(0,phostent->h_name,"hi",0);

        wsprintf(aa,"RTT: %dms,  TTL: %d",E.RoundTripTime,E.Options.Ttl );

        MessageBox(0,aa,"hi",0);

        pIcmpCloseHandle( hIP );

        FreeLibrary( hIcmp );

        WSACleanup();                                       

       return p;

}

II Servlet -Ping Program

s.java
import java.io.*;

import java.servlet.*;



public class s extends Servlet

{



public void service(ServletRequest req,ServletResponse res) throws IOException

{

        bbb a;

        res.setContentType("text/html");

        res.writeHeaders();

        PrintStream out =new PrintStream(res.getOutputStream());

        out.println("Start");

        a=new bbb();

        a.xx();

        out.println("Hop No...." + a.ctr + "<p>");

        out.println("Computer... : " + a.str + "<p>");

}



}





class bbb

{       int ctr;

        String str;

        public native void xx();

	bbb()

        {

		System.loadLibrary("sss");

	}



}

The files in the project will be
Click on Tools then Options, then Directories and set the include path to

sss.c

#include <StubPreamble.h>
#include <fcntl.h>
#include <javaString.h>
struct o

{

        unsigned char Ttl,Tos,Flags,OptionsSize,*OptionsData;

};

struct

{

        DWORD Address;

        unsigned long  Status,RoundTripTime;

        unsigned short DataSize,Reserved;

        void *Data;                  

        struct o  Options;

} E;



HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent *phostent;

DWORD d;char aa[100],bb[100];struct o I;



HANDLE ( WINAPI *pIcmpCreateFile )( VOID );

BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );

DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,LPVOID,

                                        LPVOID,DWORD,DWORD);





struct zzz

{

    long f;    

	struct Hjava_lang_String * p;

        

};



struct yyy

{

        struct zzz * oo;

        void * m;

};



long bbb_xx(struct yyy * t)

{

        char u[1000];

        hIcmp = LoadLibrary( "ICMP.DLL" );

        WSAStartup( 0x0101, &wsa );



        phostent = gethostbyname( "www.microsoft.com");

        dwIPAddr = (DWORD *)( *phostent->h_addr_list );

    

        pIcmpCreateFile=GetProcAddress( hIcmp,"IcmpCreateFile");

        pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");

        pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );



        hIP = pIcmpCreateFile();

        I.Ttl=6;

        pIcmpSendEcho(hIP,*dwIPAddr,0,0,&I,&E,sizeof(E),8000 );

        d=E.Address;

        phostent = gethostbyaddr((char *)&d,4,PF_INET);

        sprintf(aa,"gethostbyaddr %p",phostent );

        if ( phostent  != 0 )

                strcpy(aa,phostent->h_name);

		else

				strcpy(aa,"\0");

		sprintf(bb,"...RTT: %dms,  TTL: %d",E.RoundTripTime,E.Options.Ttl );

        strcat(aa,bb);

        MessageBox(0,aa,"hi",0);

        pIcmpCloseHandle( hIP );

        FreeLibrary( hIcmp );

        WSACleanup();                                       



        strcpy(u,aa);

                                t->oo->f = I.Ttl;

                t->oo->p=makeJavaString(u,strlen(u));

        return 1;

}





stack_item * Java_bbb_xx_stub(stack_item * z, void *e)

{

        bbb_xx(z->p);

        return z + 1; 

}




III Servlet - Ping Program - HTML Form

a1.html
<html>
<form action=/servlet/s method=get>
Host Computer:(www.neca.com) <br>
<input type=text name=a><p>
Hop Number :<br>
<input type=text size=5 name=b><p>
<input type=submit value="Click..."><p>
</form>
</html>
s.java
import java.io.*;

import java.servlet.*;



public class s extends Servlet

{

int aa,bb;

public void service(ServletRequest req,ServletResponse res) throws IOException

{



        String aa = req.getQueryParameter("a");

        String bb = req.getQueryParameter("b");

        res.setContentType("text/html");

        res.writeHeaders();

        PrintStream out =new PrintStream(res.getOutputStream());

        out.println("Computer... : " +aa+ "<p>");

        out.println("Hop No...." + bb + "<p>");

        bbb a;

        a=new bbb();

        a.str1=aa;

        a.str2= bb;

        a.xx();

        out.println("Result ..:" + a.str+ "<p>");

}

}





class bbb

{       String str,str1,str2;

        public native void xx();

	bbb()

        {

		System.loadLibrary("sss");

	}



}



sss.c
#include <StubPreamble.h>
#include <fcntl.h>
#include <javaString.h>
struct o

{

        unsigned char Ttl,Tos,Flags,OptionsSize,*OptionsData;

};

struct

{

        DWORD Address;

        unsigned long  Status,RoundTripTime;

        unsigned short DataSize,Reserved;

        void *Data;                  

        struct o  Options;

} E;



HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent *phostent;

DWORD d;char aa[100],bb[100];struct o I;



HANDLE ( WINAPI *pIcmpCreateFile )( VOID );

BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );

DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,LPVOID,

                                        LPVOID,DWORD,DWORD);





struct zzz

{

        struct Hjava_lang_String * p;

        struct Hjava_lang_String * p1;

        struct Hjava_lang_String * p2;

};



struct yyy

{

        struct zzz * oo;

        void * m;

};



long bbb_xx(struct yyy * t)

{

        char u[1000],u1[1000],u2[1000];

        int ii;

   

        javaString2CString(t->oo->p1,u1,1000);

       javaString2CString(t->oo->p2,u2,1000);

       ii=atoi(u2);

		MessageBox(0,u1,u2,0);

        hIcmp = LoadLibrary( "ICMP.DLL" );

        WSAStartup( 0x0101, &wsa );



        phostent = gethostbyname(u1);

        dwIPAddr = (DWORD *)( *phostent->h_addr_list );

    

        pIcmpCreateFile=GetProcAddress( hIcmp,"IcmpCreateFile");

        pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");

        pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );



        hIP = pIcmpCreateFile();

        I.Ttl=ii;

        pIcmpSendEcho(hIP,*dwIPAddr,0,0,&I,&E,sizeof(E),8000 );

        d=E.Address;

        phostent = gethostbyaddr((char *)&d,4,PF_INET);

        sprintf(aa,"gethostbyaddr %p",phostent );

        if ( phostent  != 0 )

                strcpy(aa,phostent->h_name);

        else

                strcpy(aa,"\0");

        sprintf(bb,"...RTT: %dms,  TTL: %d",E.RoundTripTime,E.Options.Ttl );

        strcat(aa,bb);

        MessageBox(0,aa,"hi",0);

        pIcmpCloseHandle( hIP );

        FreeLibrary( hIcmp );

        WSACleanup();                                       



        strcpy(u,aa);

        t->oo->p=makeJavaString(u,strlen(u));

        return 1;

}





stack_item * Java_bbb_xx_stub(stack_item * z, void *e)

{

        bbb_xx(z->p);

        return z + 1; 

}




The above tutorial is a joint effort of

Mr. Vijay Mukhi
Ms. Sonal Kotecha


Visit the top magazine devoted exclusively to Java: JavaWorld .They offer tutorials, tips and tricks,news, how-to's, and more. If you have any suggestions, comments, ideas, cracked code, feedback, feel free to get in touch with us.

Move back to the Vijay Mukhi's Technology Cornucopia Page to learn more about the other new Internet Technologies.


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Bombay 400 034, India
E-mail:vmukhi@giasbm01.vsnl.net.in Tel : 91-22-496 4335 /6/7/8/9 Fax : 91-22-307 28 59
http://www.neca.com/~vmis