欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php与flash as3 socket通信传送文件实现代码

程序员文章站 2023-10-30 15:05:40
前段时间在flashseer看到有人提到:可以通过socket方式传送swf文件,让用户无法获取到swf文件… 当时还没有出as3的反编译,所以对程序的保护没有在意.随着反...

前段时间在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 'socket_create() failed: reason: ' . socket_strerror($sock) . '\n';
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
echo 'socket_bind() failed: reason: ' . socket_strerror($ret) . '\n';
}
if (($ret = socket_listen($sock, 5)) < 0) {
echo 'socket_listen() failed: reason: ' . socket_strerror($ret) . '\n';
}
echo 'connect…………….';
do {
if (($msgsock = socket_accept($sock)) < 0) {
  echo 'socket_accept() failed: reason: ' . socket_strerror($msgsock) . '\n';
  break;
}
do {
  socket_recv($msgsock, $buf, 2048, 0);
  if ($buf == "") {
  socket_close($msgsock);
  echo "some one quit";
  break;
  } else if ($buf == "bin") {
  //读取主程序数据
  $f = fopen('main.swf','r');
  $data = fread($f,filesize('main.swf'));
  fclose($f);
  //转换为字符串
  //echo base64_encode($data);
  $talkback = base64_encode($data).'end';
  }
  //发送
  socket_send($msgsock, $talkback, strlen($talkback),0);
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);