简体   繁体   中英

Sending a html file with an <APPLET> tag over Java sockets

I have a simple java helloworld applet and a html file with a tag referencing to that applet. The whole thing works normal when I use apache server. Client computer accesses the port 80 on my server, the html with the applet tag is entered and the applet is open.

However, if I try to send this same html file over Java socket through some other port for client to open it from its browser it doesn't work, I get an applet error on the clients browser.

This is how I send a file when a socket connection is accepted:

DataOutputStream out=null;
    File f=new File("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\index.html");
    DataInputStream htmlFile;
    try {
        out = new DataOutputStream(clientSocket.getOutputStream());
        htmlFile = new DataInputStream(new FileInputStream(f));
        int len = (int) f.length();
        byte[] buf = new byte[len];
        htmlFile.readFully(buf);
        htmlFile.close();
        out.writeBytes("HTTP/1.1 200 OK\r\n");
        out.writeBytes("Content-Length: " + f.length() +"\r\n");
        out.writeBytes("Content-Type: text/html\r\n\r\n");
        out.write(buf);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And this is the error I get:

    Java Plug-in 1.6.0_30
Using JRE version 1.6.0_30-b12 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Administrator
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------


java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at sun.plugin.util.GrayBoxPainter.setProgressFilter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.setupGrayBoxPainter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.access$700(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
exception: name.
java.lang.IllegalArgumentException: name
    at sun.plugin2.applet.Applet2ClassLoader.addJar(Unknown Source)
    at sun.plugin2.applet.Applet2Manager.loadJarFiles(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.IllegalArgumentException: name
java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at sun.plugin.util.GrayBoxPainter.setProgressFilter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.setupGrayBoxPainter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.access$700(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
exception: name.
java.lang.IllegalArgumentException: name
    at sun.plugin2.applet.Applet2ClassLoader.addJar(Unknown Source)
    at sun.plugin2.applet.Applet2Manager.loadJarFiles(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.IllegalArgumentException: name
java.lang.ClassFormatError: Incompatible magic value 1013018736 in class file MyApplet/HelloWorld
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassFormatError: Incompatible magic value 1013018736 in class file MyApplet/HelloWorld

If anyone could tell me what's going or another way to do this on I'd appreciate it.

EDIT:

  1. html source code(its just the aapplet tag):

    applet code = "MyApplet.HelloWorld" archive = "HelloWorldApplet.jar", width = 300, height = 300 /

  2. Client is supposed to type in the servers ip address followed by ":portNo." into a browser. "192.168.1.100:4444"

  3. Yes, I'm aware of that. Isn't that what the applet tag is for. My jar file is tagged in the html file. If the user opens the html the jar file should be downloaded and visible in the clients browser. Isn't that how it works?

  4. I'm trying something out on android. I'd like to be able to open up an applet which my android sends on a computer which is on the same wifi network. This looked like the way to do it. Installing some web server on android seemed like an unwanted complication if thats even possible to do.

To whom it may concern.

Managed to solve this. Thanks Nizet for pointing me in the right direction, didn't really bother to see how http works until now. So in the end the point was to simulate how http handles java applets. By looking at the requests and responses on loading a normal applet I've found out that after the browser loads the html file it sends a request for the referenced jar file ( GET /HelloWorldApplet.jar HTTP/1.1 ) which I then must return in a http response such as this:

 HTTP/1.1 200 OK
 Content-Length: fileLength
 Content-Type: application/java-archive

 Then send jar file as byte buffer.

Here's the whole class:

public class MyThread extends Thread{
    Socket clientSocket;
    public MyThread(Socket socket){
        this.clientSocket=socket;
    }
    public void run(){

        System.out.println("Connection established!");

        DataOutputStream out=null;
        File h=new File("index.html");
        File j=new File("HelloWorldApplet.jar");
        DataInputStream htmlFile;
        DataInputStream jarFile;
        BufferedReader br=null;
        try {
            br=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new DataOutputStream(clientSocket.getOutputStream());
            htmlFile = new DataInputStream(new FileInputStream(h));
            jarFile=new DataInputStream(new FileInputStream(j));
            int hlen = (int) h.length();
            int jlen=(int) j.length();
            byte[] hbuf = new byte[hlen];
            byte[] jbuf = new byte[jlen];
            htmlFile.readFully(hbuf);
            jarFile.readFully(jbuf);
            htmlFile.close();
            jarFile.close();
            String request=br.readLine();
            System.out.println(request);
            if(request.startsWith("GET / HTTP/1.1")){
                out.writeBytes("HTTP/1.1 200 OK\r\n");
                out.writeBytes("Content-Length: " + h.length() + "\r\n");
                out.writeBytes("Content-Type: text/html\r\n\r\n");
                out.write(hbuf);
            }
            if(request.startsWith("GET /HelloWorldApplet.jar HTTP/1.1")){
                out.writeBytes("HTTP/1.1 200 OK\r\n");
                out.writeBytes("Content-Length: " + j.length() + "\r\n");
                out.writeBytes("Content-Type: application/java-archive\r\n\r\n");
                out.write(jbuf);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(NullPointerException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            out.close();
            clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM