0 Replies Latest reply on Jan 18, 2010 12:58 AM by anaping wan

    about the socket security problem,Plese help.

    anaping wan

      I use the port 843 server to send xml to swf.But not work. Please help. My Code is:

      Server:

      //HelloTest.java

      import java.io.IOException;

      import java.net.ServerSocket;

      import java.net.Socket;

       

      //让其继承线程类是为了更好控制 其余的线程

      public class HelloTest extends Thread {

       

      /**

      * @param args

      */

      public static void main(String[] args) {

      // TODO Auto-generated method stub

      //发送策略的服务器

      new HelloTest().start();

       

      //这个是用的fuq

      new Thread(new QtServer()).start();

       

       

      }

      @Override

      public void run()

      {

      System.out.println("策略服务器启动");

        boolean lising=true;

      ServerSocket ss;

      try {

      ss = new ServerSocket(843);

      while(lising)

        {

       

      try {

      //得到socket服务器

       

       

        Socket sc=ss.accept();

      SendPolicyFile sp=new SendPolicyFile(sc);

       

      System.out.println("为什么呢");

      new Thread(sp).start();

       

       

      } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

       

      try {

      Thread.sleep(1000);

      } catch (InterruptedException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

      }

       

        }

      ss.close();

      } catch (IOException e1) {

      // TODO Auto-generated catch block

      e1.printStackTrace();

      }

       

       

      }

       

      }

      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      import java.io.PrintWriter;
      import java.net.Socket;
      //SendPolicyFile.java
      public class SendPolicyFile implements Runnable {
         
      private Socket soc;
      private OutputStream _socketOut;
      private InputStream _socketIn;
      public SendPolicyFile(Socket s) throws IOException
      {
      this.soc=s;
      _socketOut=s.getOutputStream();
      _socketIn=s.getInputStream();
      //设置超时的限制
      //soc.setSoTimeout(1000);
      }
      public void run() {
      // TODO Auto-generated method stub
          try {
      readRandSp();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }
      @SuppressWarnings("unused")
      private void readRandSp() throws IOException
      {
        if(read().equals("<policy-file-request/>"))
        {
        System.out.println("write Policy to flash");
        writePolicy();
       
        }
              //close stream and socket
        close();
       
      }
      //写策略文件的方法
      private void writePolicy() throws IOException
      {
         String string1="<?xml version=\"utf-8\" ?><cross-domain-policy><site-control permitted-cross-domain-policies=\"master-only\"/><allow-access-from domain=\"*\" to-ports=\"8001\"/></cross-domain-policy>"+"\u0000";
      PrintWriter pw=new PrintWriter(_socketOut);
      pw.println(string1+"\u0000");
      pw.flush();
      pw.close();
      //
      System.out.println(string1+"\u0000");
      //关闭流
      //关闭socket
      }
      public void close()
      {
        if(_socketOut!=null)
      try {
      _socketOut.close();
      if(_socketIn!=null)
        _socketIn.close();
        if(soc!=null)
        soc.close();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      _socketIn=null;
      _socketOut=null;
      soc=null;
       
      }
      //发送两个Socket
      private String read() throws IOException
      {
      System.out.println("这里也执行吗");
      StringBuffer buffer=new StringBuffer();
      int codePoint;
      boolean zeroByteRead=false;
      do{
      //这个地方阻塞了
      codePoint=this._socketIn.read();
      //如果接受到的codePoint为null那证明客户端与我们失去连接了
      if(codePoint==0)
        zeroByteRead=true;
      else
      buffer.appendCodePoint(codePoint);
      }
      while(!zeroByteRead && buffer.length()<30);
      return buffer.toString();
      }
      }