2010-06-01 55 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); 
     } 
    } 

} 
相关问题