首頁  >  文章  >  php教程  >  php與flash as3 socket通訊傳送文件實現代碼

php與flash as3 socket通訊傳送文件實現代碼

高洛峰
高洛峰原創
2016-12-22 09:53:081070瀏覽

前段時間在flashseer看到有人提到:可以透過socket方式傳送swf檔,讓使用者無法取得到swf檔… 當時還沒出as3的反編譯,所以對程式的保護沒有在意.隨著反編譯程式的平民化,不希望別人看到原始碼的朋友就比較著急…

透過socket方式傳送swf檔案來避免洩漏原始碼的思路(只適用as3): 讓主程式的loader(前台swf)透過socket連接到後台程式,然後請求需要的主程式檔案.後台讀取主程式資料透過socket傳送給主程式的loader.前台swf適用Loader.loadBytes方法顯示主程式檔案.

下面是測試的來源程式:

package {
import flash.display.Sprite;
import flash.net.Socket;
import flash.events.*;
import flash.errors.IOError;
import flash.display.Loader;
 
public class SocketTest extends Sprite {
  public function SocketTest() {
  var socket = new Socket();
  socket.connect("localhost", 5277);
  socket.addEventListener(Event.CLOSE, closeHandler);
  socket.addEventListener(Event.CONNECT, connectHandler);
  socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
  var response:String;
 
  function writeln(str:String):void {
   str+="";
   try {
   socket.writeUTFBytes(str);
   } catch (e:IOError) {
   trace(e);
   }
  }
  function sendRequest():void {
   trace("sendRequest");
   response="";
   writeln('bin');
   socket.flush();
  }
  function readResponse():void {
   //接受数据
   var str:String=socket.readUTFBytes(socket.bytesAvailable);
   response+=str;
   //当数据以end结束的时候,数据传输结束
   if (StringUtil.endsWith(str,'end')) {
   response=response.substr(0,response.length-3);
   //把字符串转换为ByteArray
   var ba=Base64.decodeToByteArray(response);
   //trace('bytearray=====',ba);
   var l:Loader = new Loader();
   //从 ByteArray 对象中所存储的二进制数据中加载。
   l.loadBytes(ba);
   //trace(l.content);
   addChild(l);
   }
  }
  function closeHandler(e:Event):void {
   trace("Socket closed! ");
  }
  function connectHandler(e:Event):void {
   trace("Socket connected!");
   sendRequest();
  }
  function ioErrorHandler(e:IOErrorEvent):void {
   trace("IOError!");
  }
  function securityErrorHandler(e:SecurityErrorEvent):void {
   trace("SecurityError!");
  }
  function socketDataHandler(e:ProgressEvent):void {
   trace("Socket have data!");
   readResponse();
  }
 
  }
}
}

PHP:

//后台scoket程序,由于适用php写比较简单,就用php写了.
//error_reporting(E_ALL);
error_reporting(0);
set_time_limit(0);
ob_implicit_flush();
$address = 'localhost';
$port = 5277;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
echo &#39;socket_create() failed: reason: &#39; . socket_strerror($sock) . &#39;\n&#39;;
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo &#39;socket_bind() failed: reason: &#39; . socket_strerror($ret) . &#39;\n&#39;;
}
if (($ret = socket_listen($sock, 5)) < 0) {
echo &#39;socket_listen() failed: reason: &#39; . socket_strerror($ret) . &#39;\n&#39;;
}
echo &#39;connect…………….&#39;;
do {
if (($msgsock = socket_accept($sock)) < 0) {
  echo &#39;socket_accept() failed: reason: &#39; . socket_strerror($msgsock) . &#39;\n&#39;;
  break;
}
do {
  socket_recv($msgsock, $buf, 2048, 0);
  if ($buf == "") {
  socket_close($msgsock);
  echo "some one quit";
  break;
  } else if ($buf == "bin") {
  //读取主程序数据
  $f = fopen(&#39;main.swf&#39;,&#39;r&#39;);
  $data = fread($f,filesize(&#39;main.swf&#39;));
  fclose($f);
  //转换为字符串
  //echo base64_encode($data);
  $talkback = base64_encode($data).&#39;end&#39;;
  }
  //发送
  socket_send($msgsock, $talkback, strlen($talkback),0);
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);

更多php與flash as3 socket通訊傳送文件實現代碼相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn