java与Flash的Socket通讯

时间:2023-01-18 18:07:41

Flash与JAVA的Socket通信

import java.io.*;
import java.net.*;
public class AMFServer {
public void start()throws Exception{
  String xml="<cross-domain-policy>";
  xml+="<allow-access-from domain=/"127.0.0.1/"to-ports=/"5000,8000/">";
  xml+="<allow-access-from domain=/"localhost/"to-ports=/"5000,8000/">";
  xml+="</cross-domain-policy>";
 
  ServerSocket serverSocket=new ServerSocket(5000);
  System.out.println("等待用户连接````````");
  while(true){
   try{
    Socket socket=serverSocket.accept();
    System.out.println("连接成功```````");
    BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter pw=new PrintWriter(socket.getOutputStream());
   
    char[] by=new char[22];
    br.read(by,0,22);
    String head=new String(by);
    System.out.println("消息头是:"+head+";");
    if(head.equals("<policy-file-request/>")){
     pw.write(xml+"/0");
     pw.flush();
    }else{
     ServerThread thread=new ServerThread(socket);
     thread.start();
    }
   
   }catch(Exception e){
    System.out.println("服务器发生异常");
   }
  }
}
public static void main(String[] args) {
  try{
   new AMFServer().start();
  }catch(Exception e){
   System.out.println("Socket发生异常:"+e);
  }
}
}





import java.net.Socket;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
public class ServerThread extends Thread{
private Socket socket;
private BufferedReader input;
private PrintWriter output;
public ServerThread(Socket socket){
  this.socket=socket;
}

public void run(){
  System.out.println("---------------启动一个线程-----------------");
  try{
   input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
   output=new PrintWriter(socket.getOutputStream());
   while(true){
    String msg=input.readLine();
    if(msg==null){
     break;
    }
    System.out.println("收到客户端的数据:"+msg);
    output.print("服务器返回:"+msg+"/0");
    output.flush();
   }
   System.out.println("子线程结束了");
  }catch(Exception e){
   System.out.println("子线程发生异常");
   try{
    input.close();
    output.close();
   }catch(Exception ex){
    System.out.println("关闭资源发生异常");
   }
   System.out.println("子线程结束了");
  }
}
}






package org.Eden.net {
import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.*;
import flash.net.XMLSocket;
import flash.system.Security;
import flash.system.System;
public class EdenSockets extends Sprite{
private var socket:XMLSocket;
  public function EdenSockets()
  {
   System.useCodePage = true;
   socket = new XMLSocket();
   socket.addEventListener( Event.CONNECT, onConnect );
   socket.addEventListener( IOErrorEvent.IO_ERROR , failConnect );
   socket.addEventListener( DataEvent.DATA, onDatas );

   sendBtn.addEventListener(MouseEvent.MOUSE_DOWN,sendEvent);
   socket.connect("127.0.0.1", 5000);
  }

  public function onConnect(myStatus:Event):void
  {
   msgText.text = msgText.text + "连接成功/n";
   //立即发送成功 信息到服务器
   socket.send("<policy-file-xxquest/>/n");
  }
  public function failConnect(myStatus:IOErrorEvent):void
  {
   msgText.text = msgText.text + "连接失败/n";
  }
  public function onDatas(event:DataEvent):void
  {
   if(event.data != null)
   {
    msgText.text = msgText.text + event.data + "/n";
   }
  }

  public function sendEvent(event:MouseEvent):void
  {
   socket.send(speakText.text + "/n");
   speakText.text = "";
  }
}

}