<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" fontSize="12" creationComplete="onInit()">
<mx:ViewStack id="viewstack1" width="454" height="338" selectedIndex="0">
<mx:Canvas label="View 1" width="100%" height="100%">
<mx:TitleWindow width="100%" height="100%" layout="absolute" title="连接设置">
<mx:Button x="181" y="186" label="连 接" click="onConnect()" id="btnCon"/>
<mx:TextInput x="181" y="31" id="txtHost" text="127.0.0.1" width="184"/>
<mx:TextInput x="181" y="94" id="txtPort" width="184" text="1083"/>
<mx:Label x="74" y="31" text="链接地址:"/>
<mx:Label x="74" y="94" text="连接端口:"/>
<mx:Button x="297" y="186" label="清 空" click="onEmpty()"/>
</mx:TitleWindow>
</mx:Canvas>
<mx:Canvas label="View 1" width="100%" height="100%">
<mx:TitleWindow width="100%" height="100%" layout="absolute" title="消息窗口">
<mx:TextArea x="10" y="10" width="414" height="176" id="txtRec"/>
<mx:Button x="160" y="262" label="发 送" fontSize="12" click="onSendData()" />
<mx:Button x="260" y="262" label="断 开" fontSize="12" click="onClose()"/>
<mx:TextInput x="10" y="194" width="414" id="txtSend" height="45"/>
<mx:Text x="201" y="202" text="正在断开,请稍后..." visible="false" id="msg"/>
<mx:Button x="355" y="262" label="关 闭" click="onCloseWindow()"/>
</mx:TitleWindow>
</mx:Canvas>
</mx:ViewStack>
<mx:Script>
<![CDATA[
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
import mx.controls.Alert;
//Security.loadPolicyFile("http://localhost/crossdomain.xml");
//全局变量
private var socket:Socket=new Socket();
//连接
private function onInit():void
{
socket.addEventListener(Event.CONNECT,connect);
socket.addEventListener(Event.CLOSE,onClosed);
socket.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSeurity);
socket.addEventListener(ProgressEvent.SOCKET_DATA,onReceiveData);
}
private function connect(e:Event):void
{
this.viewstack1.selectedIndex=1;
}
private function onClosed(e:Event):void
{
}
//连接服务器
private function onConnect():void
{
var host:String=this.txtHost.text;
var port:Number = Number(this.txtPort.text);
if(!(host&&port))
{
Alert.show("服务器地址和端口不能为空!","提示");
return;
}
socket.connect(host,port);
}
//关闭服务器
private function onClose():void
{
socket.close();
this.viewstack1.selectedIndex=0;
}
//IO错误
private function onIoError(evt:IOErrorEvent):void
{
Alert.show(evt.text,"IO错误!");
}
//安全策略错误
private function onSeurity(evt:SecurityErrorEvent):void
{
Alert.show(evt.text,"安全策略错误");
}
//发送
private function onSendData():void
{
var buff:ByteArray=new ByteArray();
buff.writeUTF(this.txtSend.text);
socket.writeBytes(buff,0,buff.length);
socket.flush();//发送数据
}
//接收
private function onReceiveData(evt:ProgressEvent):void
{
while(socket.bytesAvailable)
{
this.txtRec.text+=socket.readMultiByte(socket.bytesAvailable,"utf8")+"\n";
}
}
/**ui操作**/
//清空
private function onEmpty():void
{
this.txtHost.text="";
this.txtPort.text="";
this.txtHost.setFocus();
}
//关闭浏览器
private function onCloseWindow():void
{
var request:URLRequest = new URLRequest("javascript:window.close()");
navigateToURL(request,"_self");
}
]]>
</mx:Script>
</mx:Application>
flex代码
url http://localhost/socket/bin-test/xx.html
服务端 C:\wamp\php\php.exe C:\wamp\www\socket\server\server.phpset_time_limit(0);
$address = "127.0.0.1";
ob_implicit_flush();
$port = '1083';
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";
}
do{
if(!($msgsock = socket_accept($sock))){
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";
break;
}
/* 开始接受socket传过来的数据s. */
do{
//如果是安全策略请求,则传输安全策略文件内容
if($buf = socket_read($msgsock, 2048)){
if(strpos($buf,'policy-file-request')){
$msg ="<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>";
socket_write($msgsock, $msg."/0", strlen($msg."/0"));
}
//答复数据
$talkback = "PHP:艹 You said '$buf'./n";
socket_write($msgsock, $talkback, strlen($talkback));
}
}while(true);
socket_close($msgsock);
}while(true);
跨域文件 crossdomain.xml 在根目录下 C:\wamp\www(localhost目录)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" />
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
求解总是报 Error #2048 安全策略错误 纠结啊!
求高人。。
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" fontSize="12" creationComplete="onInit()">
<mx:ViewStack id="viewstack1" width="454" height="338" selectedIndex="0">
<mx:Canvas label="View 1" width="100%" height="100%">
<mx:TitleWindow width="100%" height="100%" layout="absolute" title="连接设置">
<mx:Button x="181" y="186" label="连 接" click="onConnect()" id="btnCon"/>
<mx:TextInput x="181" y="31" id="txtHost" text="127.0.0.1" width="184"/>
<mx:TextInput x="181" y="94" id="txtPort" width="184" text="1083"/>
<mx:Label x="74" y="31" text="链接地址:"/>
<mx:Label x="74" y="94" text="连接端口:"/>
<mx:Button x="297" y="186" label="清 空" click="onEmpty()"/>
</mx:TitleWindow>
</mx:Canvas>
<mx:Canvas label="View 1" width="100%" height="100%">
<mx:TitleWindow width="100%" height="100%" layout="absolute" title="消息窗口">
<mx:TextArea x="10" y="10" width="414" height="176" id="txtRec"/>
<mx:Button x="160" y="262" label="发 送" fontSize="12" click="onSendData()" />
<mx:Button x="260" y="262" label="断 开" fontSize="12" click="onClose()"/>
<mx:TextInput x="10" y="194" width="414" id="txtSend" height="45"/>
<mx:Text x="201" y="202" text="正在断开,请稍后..." visible="false" id="msg"/>
<mx:Button x="355" y="262" label="关 闭" click="onCloseWindow()"/>
</mx:TitleWindow>
</mx:Canvas>
</mx:ViewStack>
<mx:Script>
<![CDATA[
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
import mx.controls.Alert;
//Security.loadPolicyFile("http://localhost/crossdomain.xml");
//全局变量
private var socket:Socket=new Socket();
//连接
private function onInit():void
{
socket.addEventListener(Event.CONNECT,connect);
socket.addEventListener(Event.CLOSE,onClosed);
socket.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSeurity);
socket.addEventListener(ProgressEvent.SOCKET_DATA,onReceiveData);
}
private function connect(e:Event):void
{
this.viewstack1.selectedIndex=1;
}
private function onClosed(e:Event):void
{
}
//连接服务器
private function onConnect():void
{
var host:String=this.txtHost.text;
var port:Number = Number(this.txtPort.text);
if(!(host&&port))
{
Alert.show("服务器地址和端口不能为空!","提示");
return;
}
socket.connect(host,port);
}
//关闭服务器
private function onClose():void
{
socket.close();
this.viewstack1.selectedIndex=0;
}
//IO错误
private function onIoError(evt:IOErrorEvent):void
{
Alert.show(evt.text,"IO错误!");
}
//安全策略错误
private function onSeurity(evt:SecurityErrorEvent):void
{
Alert.show(evt.text,"安全策略错误");
}
//发送
private function onSendData():void
{
var buff:ByteArray=new ByteArray();
buff.writeUTF(this.txtSend.text);
socket.writeBytes(buff,0,buff.length);
socket.flush();//发送数据
}
//接收
private function onReceiveData(evt:ProgressEvent):void
{
while(socket.bytesAvailable)
{
this.txtRec.text+=socket.readMultiByte(socket.bytesAvailable,"utf8")+"\n";
}
}
/**ui操作**/
//清空
private function onEmpty():void
{
this.txtHost.text="";
this.txtPort.text="";
this.txtHost.setFocus();
}
//关闭浏览器
private function onCloseWindow():void
{
var request:URLRequest = new URLRequest("javascript:window.close()");
navigateToURL(request,"_self");
}
]]>
</mx:Script>
</mx:Application>
flex代码
url http://localhost/socket/bin-test/xx.html
服务端 C:\wamp\php\php.exe C:\wamp\www\socket\server\server.phpset_time_limit(0);
$address = "127.0.0.1";
ob_implicit_flush();
$port = '1083';
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";
}
do{
if(!($msgsock = socket_accept($sock))){
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";
break;
}
/* 开始接受socket传过来的数据s. */
do{
//如果是安全策略请求,则传输安全策略文件内容
if($buf = socket_read($msgsock, 2048)){
if(strpos($buf,'policy-file-request')){
$msg ="<cross-domain-policy><allow-access-from domain='*' to-ports='*' /></cross-domain-policy>";
socket_write($msgsock, $msg."/0", strlen($msg."/0"));
}
//答复数据
$talkback = "PHP:艹 You said '$buf'./n";
socket_write($msgsock, $talkback, strlen($talkback));
}
}while(true);
socket_close($msgsock);
}while(true);
跨域文件 crossdomain.xml 在根目录下 C:\wamp\www(localhost目录)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" />
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
求解总是报 Error #2048 安全策略错误 纠结啊!
求高人。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货