2010-06-01 54 views
2

我試圖獲得一個生活在Web服務器上的實時Flash,以便與本地Java服務器通信,該實時Flash將位於客戶端PC上。AS3/Java - 從實時Flash到本地java的套接字連接

我想通過套接字連接實現這一點。 (端口6000) 現在,起初閃光能夠連接,但它只是發送<policy-file-request/>。此後沒有任何反應。

現在,一些在Kirupa的人建議在Java端建立任何連接後立即發送一個跨域策略xml。 http://www.kirupa.com/forum/showthread.php?t=301625

然而,我的Java服務器只是拋出了以下內容:

結束異常:java.net.SocketException異常:軟件導致連接中止:recv的失敗

我已經花了很大的在這個問題上的時間量,並想知道這裏有人知道該怎麼辦?

+2

僅供參考,港口6000-6063保留用於X Window系統:http://www.iana.org/assignments/port-numbers – 2010-06-01 15:38:55

回答

2

我發現anwser,所以生病發布它在這裏以防萬一有類似問題的人發現這篇文章。

當下閃存連接到本地socket服務器會發送以下內容:

<policy-file-request/> 

我們將有一個策略文件回答和立刻關閉連接

的Java:

import java.net.*; 
import java.io.*; 

public class NetTest { 

    public static void main(String[] args) { 
     ServerSocket serverSocket = null; 

     /* Open a socket to listen */ 
     try { 
      serverSocket = new ServerSocket(6000); 
     } catch (IOException e) { 
      System.out.println("Could not listen on port: 6000"); 
      System.exit(-1); 
     } 

     // Try catch a socket to listen on 
     Socket clientSocket = null; 
     try { 
      System.out.println("Waiting for auth on 6000..."); 
      clientSocket = serverSocket.accept(); 
     } catch (IOException e) { 
      System.out.println("Accept failed: 6000"); 
      System.exit(-1); 
     } 

     // Now a stream has been opened... 
     InputStream in = null; 
     OutputStream out = null; 
     try { 
      in = clientSocket.getInputStream(); 
      out = clientSocket.getOutputStream(); 
     } catch (IOException e) { 
      System.out.println("Failed to get streams."); 
      System.exit(-1); 
     } 

     System.out.println("Socket connection incoming!"); 

     // Keep going while we can... 
     byte b[] = new byte[100]; 
     int offset = 0; 
     String s; 
     try { 
      boolean done = false; 
      boolean auth = false; 
      String protocol_target = "<policy-file-request/>"; 
      byte[] p_bytes = protocol_target.getBytes(); 
      int result; 
      while (!done) { 
       if (in.read(b, offset, 1) == -1) 
        done = true; 
       else { 
        if (!auth) { 
         ++offset; 
         b[offset] = 0; 
         if (offset != p_bytes.length) { 
          System.out.println("Waiting for protocol data... (" 
            + offset + "/" + p_bytes.length + ")"); 
         } else { 
          // Compare byte data 
          for (int i = 0; i < p_bytes.length; ++i) { 
           System.out.print(b[i] + " "); 
          } 
          System.out.print("\n"); 
          System.out.flush(); 
          for (int i = 0; i < p_bytes.length; ++i) { 
           System.out.print(p_bytes[i] + " "); 
          } 
          System.out.print("\n"); 
          System.out.flush(); 
          boolean match = true; 
          for (int i = 0; i < p_bytes.length; ++i) { 
           if (b[i] != p_bytes[i]) { 
            match = false; 
            System.out 
              .println("Mismatch on " + i + "."); 
           } 
          } 
          if (match) 
           auth = true; 
          else { 
           System.out.println("Bad protocol input."); 
           System.exit(-1); 
          } 
         } 

         // Auth 
         if (auth) { 
          System.out.println("Authing..."); 
          s = "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain='*' to-ports='6000' /></cross-domain-policy>"; 
          b = s.getBytes(); 
          out.write(b, 0, b.length); 
          b[0] = 0; 
          out.write(b, 0, 1); // End 
          out.flush(); 
          offset = 0; 
          b = new byte[100]; 
          b[0] = 0; 
          auth = true; 
          System.out.println("Auth completed."); 
         } 
        } 
       } 
      } 
     } catch (IOException e) { 
      System.out.println("Stream failure: " + e.getMessage()); 
      System.exit(-1); 
     } 

     // Finished. 
     try { 
      in.close(); 
      out.close(); 
      clientSocket.close(); 
     } catch (Exception e) { 
      System.out.println("Failed closing auth stream: " + e.getMessage()); 
      System.exit(-1); 
     } 

     // Try catch a socket to listen on for data 
     try { 
      System.out.println("Waiting on 6000 fo data..."); 
      clientSocket = serverSocket.accept(); 
     } catch (IOException e) { 
      System.out.println("Accept failed: 6000"); 
      System.exit(-1); 
     } 

     // Now a stream has been opened... 
     in = null; 
     out = null; 
     try { 
      in = clientSocket.getInputStream(); 
      out = clientSocket.getOutputStream(); 
     } catch (IOException e) { 
      System.out.println("Failed to get streams."); 
      System.exit(-1); 
     } 

     System.out.println("Socket data connection waiting."); 

     // Echo 
     try { 
      boolean done = false; 
      while (!done) { 
       if (in.read(b, offset, 1) == -1) 
        done = true; 
       else { 
        b[1] = 0; 
        s = new String(b); 
        System.out.print(s); 
        System.out.flush(); 
       } 
      } 
     } catch (IOException e) { 
      System.out.println("Failed echo stream: " + e.getMessage()); 
      System.exit(-1); 
     } 

     // Finished. 
     try { 
      in.close(); 
      out.close(); 
      clientSocket.close(); 
      serverSocket.close(); 
     } catch (Exception e) { 
      System.out.println("Failed closing stream: " + e.getMessage()); 
      System.exit(-1); 
     } 
    } 

} 
相關問題