2012-01-22 15 views
2

私は単純なjava helloworldアプレットとそのアプレットを参照するタグを持つhtmlファイルを持っています。私はApacheサーバーを使用すると、すべて正常に動作します。クライアントコンピュータは私のサーバ上のポート80にアクセスし、アプレットタグ付きhtmlが入力され、アプレットが開いています。<APPLET>タグ付きのhtmlファイルをJavaソケット経由で送信

しかし、この同じhtmlファイルをJavaソケット経由でクライアントの別のポート経由で送信しようとすると、ブラウザから開くことができません。クライアントブラウザでアプレットエラーが発生します。

この

は、私はソケット接続が受け入れられているファイルの送信方法です:

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(); 
    } 

をそして、これは私が取得エラーです:誰もが私に言うことができれば何が起こっているのか

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 

または別の方法私はそれを感謝してこれを行うには。

EDIT:

  1. HTMLソースコード(そのわずかaappletタグ):

    アプレットコード= "MyApplet.HelloWorld" アーカイブ= "HelloWorldApplet.jar"、幅= 300、高さ= 300/

  2. クライアントはサーバのIPアドレスに「:portNo」を続けて入力すると仮定します。ブラウザに入れます。 "192.168.1.100:4444"

  3. はい、私はそれを認識しています。それはアプレットタグのためのものではありません。私のjarファイルはhtmlファイルでタグ付けされています。ユーザーがhtmlを開くとjarファイルがダウンロードされ、クライアントブラウザに表示されます。それはどういう仕組みですか?

  4. 私はアンドロイドで何かを試しています。私はアンドロイドが同じ無線LANネットワーク上にあるコンピュータで送信するアプレットを開くことができるようにしたいと思います。これはそれを行う方法のように見えました。アンドロイドにいくつかのWebサーバーをインストールすることは、可能な場合でも不要な複雑さのように思えました。

+1

HTMLファイルのソースコードは何ですか?クライアントはソケットから受け取るHTMLページをどのように開いていますか?アプレット自体(jarとその依存関係)もダウンロードする必要があることをご存知ですか?なぜあなたはHTTPを使わずにHTMLを提供したいのですか? –

+0

@JBNizet 4つの優秀な質問。 –

+0

edit ........ – user1154267

答えて

0

これを解決するために管理されました。正しい方向に私を指してくれてありがとうございましたNizetは、今までhttpがどのように動作しているか気にしませんでした。最終的には、httpがJavaアプレットをどのように処理するかをシミュレートすることがポイントでした。通常のアプレットのロード時に要求と応答を調べると、ブラウザーがHTMLファイルをロードした後、参照されたjarファイル(GET /HelloWorldApplet.jar HTTP/1.1)の要求が送信されることがわかりましたこのようなHTTPレスポンスでリターン:

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

Then send jar file as byte buffer. 

ここでは、クラス全体です:

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(); 
     } 

    } 
} 
関連する問題