FLASH与服务器通讯 (JAVA)(2)

Socket socket;
    final int BYTE_ARRAY_MAX_LEN = 1000;
    final String FLASH_SAFE = "<policy-file-request/>";
    static String safe = "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-

policy>\0";
    Sender sender;
    public Receiver(Socket socket,Sender sender )
    {
        this.socket = socket;
        this.sender = sender;
    }
   
    @Override
    public void run()
    {
        try
        {
            InputStream is = socket.getInputStream();
            byte [] reader = new byte [BYTE_ARRAY_MAX_LEN];
            BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(socket.getOutputStream()));
            OutputStream out = socket.getOutputStream();
            sender.addOutput(out);
            int ch=0;
            String s;           
            while ( is.read(reader) != -1 )
            {
                //System.out.println("ch - "+ch);
                s = new String(reader);
                System.out.println("reader len:"+reader.length);
                System.out.println(s);
               
                if ( s!=null && s.indexOf("<policy-file-request/>")>=0 )//安全策略
                {
                    for ( int i=0;i<s.length();i++ )
                    {
                        System.out.println(i+" "+(int)s.charAt(i));
                    }

bw.write(safe,0,safe.length());
                    bw.flush();
                    //bw.close();
                    System.out.println(safe);
                }
                else
                {
                    synchronized(sender)
                    {
                        sender.send(s);
                    }
                }
                reader = new byte [BYTE_ARRAY_MAX_LEN];
            }
            bw.close();
            synchronized (sender)
            {
                sender.removeOutput(out);
            }
        }
        catch (Exception ex)
        {
            System.out.println("error in receiver");
            ex.printStackTrace();
        }
        try
        {
            socket.close();
            System.out.println(socket.getInetAddress()+" disconnected");
        } catch (IOException ex1) {
            System.out.println("socket close error");
            Logger.getLogger(Receiver.class.getName()).log(Level.SEVERE, null, ex1);
        }
       
    }
}
?

sender:

?

package for_flash_socket;
import java.util.*;
import java.io.*;
public class Sender {

ArrayList outputArrayList;
    public Sender()
    {
        outputArrayList = new ArrayList();
    }

public void addOutput(OutputStream out)
    {
        outputArrayList.add(out);
    }

public void removeOutput(OutputStream out)
    {
        outputArrayList.remove(out);
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpwyg.html